自定义控件
1,定义xml属性,在atts.xml写相应的属性,部分属性可带默认值
2,创建自定义控件类,通过TypedArray对象来找到响应的属性并赋值给响应成员变量属性、
3,在Main_xml中加载自定义控件,并设置响应属性值即可。
4,按钮方法回调机制
1,创建接口,并定义相应的方法
2,定义接口的成员变量
3,暴露方法给调用者,利用参数初始化成员变量
4,在相应的方法中利用成员变量来调用接口方法
5,调用者在接口实例中调用接口方法来操作逻辑
定义stts
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="titleText" format="string" />
<attr name="titleTextSize" format="dimension" />
<attr name="titleTextColor" format="color"/>
<attr name="leftText" format="string"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
</declare-styleable>
</resources>
自定义控件类
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 String title;
private float titleTextSize;
private int titleTextColor;
private LayoutParams leftParams,rightParams,titleParams;
//映射调用者传入的接口
private topbarClickListener listener;
/**
* 定义回调接口
*/
public interface topbarClickListener{
public void leftClick();
public void rightClick();
}
/**
* 暴露方法给调用者
* @param listener
*/
public void setOnTopbarClickListener(topbarClickListener listener){
this.listener = listener;
}
public Topbar(final 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);
titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
title = ta.getString(R.styleable.Topbar_titleText);
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.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setText(title);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xfff59563);
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton, leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightButton, rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle, titleParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
// <span style="white-space:pre"> </span>Toast.makeText(context, "点击", Toast.LENGTH_SHORT).show();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
<span style="font-family: Arial, Helvetica, sans-serif;">//</span><span style="font-family: Arial, Helvetica, sans-serif;">Toast.makeText(context, "点击" ,Toast.LENGTH_SHORT).show();</span>
}
});
}
xml
<com.shenshen.myui.Topbar
xmlns:cccc="http://schemas.android.com/apk/res-auto"
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="48dp"
cccc:leftText="返回"
cccc:leftBackground="#ff0000"
cccc:leftTextColor="#000000"
cccc:titleText="自定义控件"
cccc:titleTextColor="#ffffff"
cccc:titleTextSize="24sp"
cccc:rightText="选择"
cccc:rightBackground="#00ff00"
cccc:rightTextColor="#000000">
</com.shenshen.myui.Topbar>
MainActivity
Topbar topbar = (Topbar) findViewById(R.id.topbar);
topbar.setOnTopbarClickListener(new Topbar.topbarClickListener() {
@Override
public void leftClick() {
Toast.makeText(MainActivity.this, "点击左边", Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this, "点击右边", Toast.LENGTH_SHORT).show();
}
});
topbar.setLeftIsVisable(false);