1.做法:
1)设计需要的属性
2)实现一个View
3)引用View
2.在values中建立一个atts.xml文件
<?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="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftText" format="string"/>
<attr name="rightText" format="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
</declare-styleable>
</resources>
3.新建一个TopBar.java文件
public class TopBar extends RelativeLayout {
private Button leftButton;
private Button rightButton;
private TextView tvtitle;
private int leftColor;
private Drawable leftBackground;
private String leftText;
private int rightColor;
private Drawable rightBackground;
private String rightText;
private String titleText;
private int titleTextColor;
private float titleTextSize;
private LayoutParams leftParams;
private LayoutParams rightParams;
private LayoutParams titleParams;
private topbarClickListener listener;// 开始定义自定义监听事件
public interface topbarClickListener {
public void leftClick();
public void rightClick();
}
public void setOnTopbarClickListener(topbarClickListener listener) {
this.listener = listener;
}
public TopBar(final Context context, AttributeSet attrs) {
super(context, attrs);
// 通过TypedArray获取自定义存储的值
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.Topbar);
leftColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
leftText = ta.getString(R.styleable.Topbar_leftText);
leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
rightColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
rightText = ta.getString(R.styleable.Topbar_rightText);
rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
titleText = ta.getString(R.styleable.Topbar_titleText);
titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
ta.recycle();// 回收TypedArray,以便后面重用。
// 详见http://www.cnblogs.com/kissazi2/p/4049982.html
leftButton = new Button(context);
rightButton = new Button(context);
tvtitle = new TextView(context);
leftButton.setTextColor(leftColor);
leftButton.setText(leftText);
leftButton.setBackground(leftBackground);
rightButton.setText(rightText);
rightButton.setTextColor(rightColor);
rightButton.setBackground(rightBackground);
tvtitle.setText(titleText);
tvtitle.setTextColor(titleTextColor);
tvtitle.setTextSize(titleTextSize);
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.MATCH_PARENT,
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();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
}
public void setLeftIsVisable(boolean flag) {
if (flag) {
leftButton.setVisibility(View.VISIBLE);
} else {
leftButton.setVisibility(View.GONE);
}
}
public void setRightIsVisable(boolean flag) {
if (flag) {
rightButton.setVisibility(View.VISIBLE);
} else {
rightButton.setVisibility(View.GONE);
}
}
}
由上述代码可以看出,先是继承RelativeLayout,并重写构造方法,在方法中将自定义获取的值存到TypedArray中,并取出对应的值赋给相应的变量,再是通过LayoutParams()方法把控件添加到ViewGroup中,所有属性在LayoutParams()中设置。
其中写到了如何自定义监听事件:
首先,通过接口回调来抽象定义我们的方法,并通过接口实现将TopBar中点击事件抽象出来,让调用者决定我们做什么;
其次,通过在TopBar中暴露出来的公共方法,用代码定义我们需要的回调的控制方法;
最后,通过自定义属性和自定义方法,一静一动两种方式来控制TopBar。
在TopBar.java文件中已经写好了自定义监听事件topbarClickListener接口,这样的好处是使其变成了一个母板,不用再重复修改。
4.在activity_main.xml中添加自定义TopBar
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.topbar"
android:padding="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<com.example.topbar.TopBar
android:id="@+id/topBar1"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftBackground="@drawable/blue_button"
custom:leftText="后退"
custom:leftTextColor="#FFFFFF"
custom:rightBackground="@drawable/blue_button"
custom:rightText="下一步"
custom:rightTextColor="#ffffff"
custom:titleText="自定义标题"
custom:titleTextColor="#123412"
custom:titleTextSize="10sp" >
</com.example.topbar.TopBar>
</RelativeLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TopBar topbar = (TopBar) findViewById(R.id.topbar);
topbar.setOnTopbarClickListener(new topbarClickListener() {
@Override
public void rightClick() {
Toast.makeText(MainActivity.this, "你点击的是右键", Toast.LENGTH_SHORT)
.show();
}
@Override
public void leftClick() {
Toast.makeText(MainActivity.this, "你点击的是左键", Toast.LENGTH_SHORT)
.show();
}
});
topbar.setLeftIsVisable(false);
}
}