不能识别自定义控件中的属性,自定义控件的三个子控件也不能正常显示?
package com.jia.mytopbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Administrator on 2016/8/29.
*/
public class TopBar extends RelativeLayout {
private Button leftButton,rightButton;
private TextView tvTitle;
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
private float centerTitleTextSize;
private int centerTitleTextColor;
private String centerTitle;
private LayoutParams leftParams,rightParams,centerTitleParams;
//重写构造方法
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
//取值
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
leftText = ta.getString(R.styleable.TopBar_leftText);
rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
rightText = ta.getString(R.styleable.TopBar_rightText);
centerTitleTextSize = ta.getDimension(R.styleable.TopBar_centerTitleTextSize, 0);
centerTitleTextColor = ta.getColor(R.styleable.TopBar_centerTitleTextColor, 0);
centerTitle = ta.getString(R.styleable.TopBar_centerTitle);
ta.recycle();
//组合模式,定义已有组件,拼合
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);
leftButton.setText(leftText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
rightButton.setText(rightText);
tvTitle.setText(centerTitle);
tvTitle.setTextColor(centerTitleTextColor);
tvTitle.setTextSize(centerTitleTextSize);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xFFF59563);
//把一个控件添加到ViewGroup
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton,rightParams);
centerTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
centerTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvTitle,centerTitleParams);
}
}
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jia.mytopbar.MainActivity"> <com.jia.mytopbar.TopBar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="40dp"> custom:leftBackground="#ff08dd55" custom:leftText="Back" custom:leftTextColor="#ffffff" custom:rightBackground="#ff08dd55" custom:rightText="Back" custom:rightTextColor="#ffffff" custom:centerTitle="自定义标题" custom:centerTitleTextColor="#123456" custom:titleTextSize="16sp" </com.jia.mytopbar.TopBar> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="centerTitle" format="string" />
<attr name="centerTitleTextSize" format="dimension" />
<attr name="centerTitleTextColor" format="color" />
<attr name="leftTextColor" format="color" />
<attr name="leftBackground" format="reference|color" />
<attr name="leftText" format="string" />
<attr name="rightTextColor" format="color" />
<attr name="rightBackground" format="reference|color" />
<attr name="rightText" format="string" />
</declare-styleable>
</resources>