第一步:定义属性—-在res下的values目录下创建一个attrs.xml的文件,定义一些自己用到的属性
有关属性的详解
<resources>
<declare-styleable name="TopBar">
<attr name="title" format="string"></attr>
<attr name="titleTextSize" format="dimension"></attr>
<attr name="titleTextColor" format="color"></attr>
<attr name="leftTextColor" format="color"></attr>
<attr name="leftBackground" format="reference|color"></attr>
<attr name="leftText" format="string"></attr>
<attr name="rightTextColor" format="color"></attr>
<attr name="rightBackground" format="reference|color"></attr>
<attr name="rightText" format="string"></attr>
</declare-styleable>
</resources>
定义完属性后,新建一个自定义的类继承ViewGroup.这里继承至RelativeLayout
/**
* 组合控件
*/
public class CompoundControl extends RelativeLayout{
private topbarClickListener topbarClickListener;
private Button leftButton,rightButton;
private TextView mTextTitle;
private String leftText,mRightText,mTitleText;
private int leftTextColor,mRightTextColor,mTitleTextColor;
private float mTitleTextSize;
private Drawable mRightBackground,mLeftBackground;
private LayoutParams LeftlayoutParams,rightLayoutParams,TitleLayoutParams;
public CompoundControl(Context context) {
super(context);
}
public CompoundControl(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public CompoundControl(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
/**
* 获取自定义的属性 设置属性参数
* @param context
* @param attrs
*/
private void init(Context context,AttributeSet attrs){
/**
* 第一步引用自定义属性
*/
TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.TopBar);
/**
* 赋值
*/
leftTextColor = array.getColor(R.styleable.TopBar_leftTextColor,0);
leftText=array.getString(R.styleable.TopBar_leftText);
mLeftBackground=array.getDrawable(R.styleable.TopBar_leftBackground);
mRightBackground=array.getDrawable(R.styleable.TopBar_rightBackground);
mRightText=array.getString(R.styleable.TopBar_rightText);
mRightTextColor=array.getColor(R.styleable.TopBar_rightTextColor,0);
mTitleText=array.getString(R.styleable.TopBar_title);
mTitleTextColor=array.getColor(R.styleable.TopBar_titleTextColor,0);
mTitleTextSize=array.getDimension(R.styleable.TopBar_titleTextSize,10);
/**
* 记得调用recycler避免重新创建的时候错误
*/
array.recycle();
/**
* 自定义三个控件
*/
leftButton=new Button(context);
rightButton=new Button(context);
mTextTitle=new TextView(context);
leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(mLeftBackground);
rightButton.setText(mRightText);
mTextTitle.setText(mTitleText);
mTextTitle.setTextColor(mTitleTextColor);
mTextTitle.setTextSize(mTitleTextSize);
mTextTitle.setGravity(Gravity.CENTER);
/**
* 设置控件的大小
*/
LeftlayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LeftlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
/**
* 添加到ViewGroup
*/
addView(leftButton,LeftlayoutParams);
rightLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton,rightLayoutParams);
TitleLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
TitleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(mTextTitle,TitleLayoutParams);
setOnClickListener();
}
/**
* 设置点击事件 我在这里只写了左边的点击事件
* 自定义一个接口 通过映射接口去调用实现方法
*/
private void setOnClickListener(){
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
topbarClickListener.leftClick();
}
});
}
/**
* 给外部提供一个调用的方法
* @param on
*/
public void setTopbarClickListener(topbarClickListener on
){
this.topbarClickListener=on;
}
点击事件接口
public interface topbarClickListener {
void leftClick();
void rightClick();
}
引用自定义的类—注意名字空间 xmlns:cumtor可以随便起名
<wifi.yeeloor.com.scoller.CompoundControl
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cumtor="http://schemas.android.com/apk/res-auto"
android:id="@+id/topBar"
cumtor:leftText="Back"
cumtor:leftTextColor="#000000"
cumtor:title="自定义标题"
cumtor:titleTextSize="15sp"
cumtor:titleTextColor="#123412"
cumtor:rightText="More"
android:layout_width="match_parent"
android:layout_height="40dp"></wifi.yeeloor.com.scoller.CompoundControl>
然后通过引用<include>
调用到相应的位置
<include layout="@layout/topbar" android:id="@+id/mainTopbar"></include>