您的位置:首页 > 运维架构 > 网站架构

3.5Android控件架构与自定义控件详解之自定义View(三)

2016-03-22 20:36 656 查看

3.5.2 创建复合控件

    创建复合空间可以很好的创建出具有重用功能的空间集合,这种方式通常需要继承一个合适的ViewGroup,在给它添加指定功能的控件,从而组合成新的复合控件。
     以TopBar为例,标题栏
     需要创一个UI模版,还要有丰富的接口,可以改变模版的文字颜色行为等信息。
3.5.2.1 定义属性
     res资源目录下的Values文件夹创一个attrs.xml的属性定义文件,代码如下:
     
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="title" format="string" />
<attr name="titleTextSize" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="lefeTextColor" format="color" />
<attr name="leftBackground" format="reference" />
<attr name="leftText" format="string" />
<attr name="rightTextColor" format="color" />
<attr name="rightBackground" format="reference" />
<attr name="rightText" format="string" />
</declare-styleable>
</resources>


自定义如下:
private int leftTextColor;
private Drawable leftBackground;
private String leftText;

private float titleTextSize;
private int titleTextColor;
private String titleText;

private int rightTextColor;
private Drawable rightBackground;
private String rightText;

private Button mLeftButton, mRightButton;
private TextView mTitleText;

private LayoutParams leftLayoutParams, TitleLayoutParams, rightLayoutParams;

private myTopBarClickListener myTopBarClickListener;

//自定义click监听回调
public interface myTopBarClickListener {

public void leftClick();

public void rightClick();
}

//自定义一个监听事件方法

public void setonTopBarClickListener(myTopBarClickListener myTopBarClickListener) {
this.myTopBarClickListener = myTopBarClickListener;

}

public MyTopBar(Context context) {
super(context);
}

public MyTopBar(Context context, AttributeSet attrs) {
super(context, attrs);

//通过这个方法,将你在attrs.xml中自定义的declare-styleable的所有属性的值存放在TypedArray中
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
//从TypedArray中获取对应的值来为要设置的属性赋值
leftTextColor = typedArray.getColor(R.styleable.TopBar_lefeTextColor, 0);
leftBackground = typedArray.getDrawable(R.styleable.TopBar_leftBackground);
leftText = typedArray.getString(R.styleable.TopBar_leftText);

rightTextColor = typedArray.getColor(R.styleable.TopBar_rightTextColor, 0);
rightBackground = typedArray.getDrawable(R.styleable.TopBar_rightBackground);
rightText = typedArray.getString(R.styleable.TopBar_rightText);

titleTextColor = typedArray.getColor(R.styleable.TopBar_titleTextColor, 0);
titleText = typedArray.getString(R.styleable.TopBar_title);
titleTextSize = typedArray.getDimension(R.styleable.TopBar_titleTextSize, 10);
//获取完TypedArray的值后,一般要调用recyle方法来避免重新创建的时候的错误
//完成资源的回收
typedArray.recycle();

//动态添加控件的方式
mLeftButton = new Button(context);
mRightButton = new Button(context);
mTitleText = new TextView(context);

mRightButton.setTextColor(rightTextColor);
mRightButton.setBackgroundDrawable(rightBackground);
mRightButton.setText(rightText);

mLeftButton.setText(leftText);
mLeftButton.setTextColor(leftTextColor);
mLeftButton.setBackgroundDrawable(leftBackground);

mTitleText.setText(titleText);
mTitleText.setTextColor(titleTextColor);
mTitleText.setTextSize(titleTextSize);
mTitleText.setGravity(Gravity.CENTER);

//组件元素设置相应的布局元素
leftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
//添加到ViewGroup中
addView(mLeftButton, leftLayoutParams);

rightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(mRightButton, rightLayoutParams);

TitleLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
TitleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(mTitleText, TitleLayoutParams);

//设置监听事件
mLeftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myTopBarClickListener.leftClick();
}
});
mRightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myTopBarClickListener.rightClick();
}
});

}

//设置Button是否显示

public void setLeftIsVisible(boolean flag) {
if (flag) {
mLeftButton.setVisibility(View.VISIBLE);
} else {
mLeftButton.setVisibility(View.GONE);
}

}

public void setRightIsVisible(boolean flag) {
if (flag) {
mRightButton.setVisibility(View.VISIBLE);
} else {
mRightButton.setVisibility(View.GONE);
}

}


还有非常重要的是引用UI模版,
Android studio 中用如下代码:
xmlns:custom="http://schemas.android.com/apk/res-auto"
这样我们就能引用自定义的TopBar了

更方便的是,我们可以将这个UI模版写到一个布局文件中,然后打上<include>的标签。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自定义TopBar