本文是阅读《android 群英传》的读书笔记,实现了一个自定义的标题栏TopBar:
1.先写attr.xml文件
在AS中value文件夹下,新建一个attr.xml文件,这里需要注意的是要使用declare-styleable标签。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="title" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="color|reference"/>
<attr name="leftText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
</declare-styleable>
</resources>
2.写TopBar的java文件,实现继承相关的Layout;
思路:1、获取xml文件的中的属性值 2、初始化控件,并设置控件的属性 3、编写内部接口,使用接口回调。
public class TopBar extends RelativeLayout {
private Button mLeftButton;
private Button mRightButton;
private TextView mTitleView;
// 标题的属性值,即我们在attr.xml文件中定义的属性
private String mTitle;
private float mTitleTextSize;
private int mTitleTextColor;
// 左边的按钮
private int mLeftTextColor;
private Drawable mLeftBackgroud;
private String mLeftText;
// 右边的按钮
private int mRightColor;
private Drawable mRightBackground;
private String mRightText;
private topbarClickListener mListener;
// 布局参数
private RelativeLayout.LayoutParams mLeftParams,mRightParams,mTitleParams;
public TopBar(Context context) {
super(context);
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
//设置topbar 的背景
setBackgroundColor(0xff59563);
// 获取参数
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
mTitle = ta.getString(R.styleable.TopBar_title);
mTitleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,10.0f);
mTitleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
mRightText = ta.getString(R.styleable.TopBar_rightText);
mRightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
mRightColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
mLeftText = ta.getString(R.styleable.TopBar_leftText);
mLeftBackgroud = ta.getDrawable(R.styleable.TopBar_leftBackground);
mLeftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
//避免重新创建的时候的错误
ta.recycle();
// 初始化控件
mLeftButton = new Button(context);
mRightButton = new Button(context);
mTitleView = new TextView(context);
// 设置样式参数
mLeftButton.setText(mLeftText);
mLeftButton.setBackgroundDrawable(mLeftBackgroud);
mLeftButton.setTextColor(mLeftTextColor);
mRightButton.setText(mRightText);
mRightButton.setBackgroundDrawable(mRightBackground);
mRightButton.setTextColor(mRightColor);
mTitleView.setText(mTitle);
mTitleView.setTextColor(mTitleTextColor);
mTitleView.setTextSize(mTitleTextSize);
// 设置控件宽高
mLeftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
// 设置布局参数
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
// 添加到ViewGroup
addView(mLeftButton,mLeftParams);
mRightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
//添加到viewGroup
addView(mRightButton,mRightParams);
mTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT);
addView(mTitleView,mTitleParams);
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mListener.leftClick();
}
});
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mListener.rightClick();
}
});
}
public void setmListener(topbarClickListener mListener) {
this.mListener = mListener;
}
// 接口回调
public interface topbarClickListener{
//左按按钮点击事件
void leftClick();
// 右按按钮点击事件
void rightClick();
}
public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
3.在项目中使用自定义控件
分为两个地方:
1.在布局文件中编写控件的属性,注意在布局文件的根节点上一定要调用custom命名空间。
xmlns:custom="http://schemas.android.com/apk/res-auto"
2.在Activity中调用,这个和系统的控件调用没有太大区别。
<com.tops.qjg.learncustomview.TopBar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftBackground="@drawable/blue_button"
custom:leftText="Back"
custom:leftTextColor="#ffffff"
custom:rightBackground="@drawable/blue_button"
custom:rightText="..."
custom:rightTextColor="#ffffff"
custom:title="自定义标题"
custom:titleTextColor="#123412"
custom:titleTextSize="10sp"
>
</com.tops.qjg.learncustomview.TopBar>
4.值得学的地方
a、接口回调
b、编写的整个顺序