android基础之自定义控件-topbar

说起自定义控件,其实见到了不少,就是缺少总结,下面我总价一个例子——Topbar。


想要自定义一个控件,我们可以模仿android系统是怎么定义的。


1.在values下创建attrs.xml文件,声明自定义的属性。
attrs.xml如下:


<?xml version="1.0" encoding="utf-8"?>
<resources>


    <declare-styleable name="Topbar">
        <attr name="title" format="string|reference" />
        <attr name="titleTextSize" format="dimension|reference" />
        <attr name="titleTextColor" format="color|reference" />
        
        <attr name="leftTextColor" format="color|reference" />
        <attr name="leftBackground" format="color|reference" />
        <attr name="leftText" format="string|reference" />
        
        <attr name="rightTextColor" format="color|reference" />
        <attr name="rightBackground" format="color|reference" />
        <attr name="rightText" format="string|reference" />
    </declare-styleable>


</resources>
name是我们自定义的属性名,format是指定它的值类型(reference是资源引用)


2.创建自定义控件的.java文件。
因为Topbar是一个布局,我让它继承自系统的RelativeLayout,模仿系统的控件的
    写法,在一个控件的.java文件,有构造函数,基本控件,与attrs.xml中自定义属性的映射变量,还有定义接口,
    利用接口回调机制实现动态事件(我理解的也不是很深,多做就理解啦)。
    Topbar.java如下:
//继承自RelativeLayout布局,布局模板!
public class Topbar extends RelativeLayout {
//三个基本控件
private Button mBtnLeft, mBtnRight;
private TextView mTvTitle;
//9个映射过来的属性变量
private int mLeftTextColor, mRightTextColor, mTitleTextColor;


private String mStrTitle, mStrLeftText, mStrRightText;


private float mTitleTextSize;


private Drawable mLeftBackground, mRightBackground;
//布局参数
private LayoutParams mLeftParams, mRightParams, mTitleParams;
//接口,回调机制的实现接口
private topbarClickListener listener;
//让调用者自己实现接口中的两个方法,在Topbar是指回调
public interface topbarClickListener {
void leftClick();


void rightClick();
}


public void setOnTopbarClickListener(topbarClickListener listener) {
this.listener = listener;
}


public Topbar(final Context c, AttributeSet attrs) {
super(c, attrs);
// 通过这个方法获取自定义的属性
TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.Topbar);
// android中是通过R.styleabl.控件名_属性来获取这个属性值
mLeftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
mLeftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
mStrLeftText = ta.getString(R.styleable.Topbar_leftText);


mRightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
mRightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
mStrRightText = ta.getString(R.styleable.Topbar_rightText);


mTitleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
mTitleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
mStrTitle = ta.getString(R.styleable.Topbar_title);


// 回收资源
ta.recycle();


mBtnLeft = new Button(c);
mBtnRight = new Button(c);
mTvTitle = new TextView(c);


mBtnLeft.setTextColor(mLeftTextColor);
mBtnLeft.setBackground(mLeftBackground);
mBtnLeft.setText(mStrLeftText);


mBtnRight.setTextColor(mRightTextColor);
mBtnRight.setBackground(mRightBackground);
mBtnRight.setText(mStrRightText);


mTvTitle.setText(mStrTitle);
mTvTitle.setTextColor(mTitleTextColor);
mTvTitle.setTextSize(mTitleTextSize);
mTvTitle.setGravity(Gravity.CENTER);
// 设置Topbar的背景颜色
setBackgroundColor(0xFFF59563);


mLeftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);// 设置对齐方式
addView(mBtnLeft, mLeftParams);// 把mBtnLeft以mLeftParams的形式加入到Topbar中


mRightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(mBtnRight, mRightParams);


mTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(mTvTitle, mTitleParams);


mBtnLeft.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
listener.leftClick();
}
});
mBtnRight.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
listener.rightClick();
}
});
}
//提供共有的方法给调用者使用
public void setLeftVisible(boolean flag) {
if (flag) {
mBtnLeft.setVisibility(View.VISIBLE);
} else {
mBtnLeft.setVisibility(View.INVISIBLE);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值