【前言】
刚刚看完幕课网老师关于自定义VIEW topbar的课程,特此总结一下学习收获。
【开发环境】
Eclipse +Android Developer Tools+bluestack调试
**
一.在res/values下创建atts.xml
**
添加左右中三个控件的属性。
<resources>
<declare-styleable name="TopBar">
<attr name = "title" 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>
二.新建一个类继承自RelativeLayout
(1).声明属性。
private int leftTextColor;
private Drawable leftBackbound;
private String leftText;
//.....其余省略
(2)利用TypeArray的方法将attrs中的属性绑定。
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
leftBackbound = ta.getDrawable(R.styleable.TopBar_leftBackground);
leftText = ta.getString(R.styleable.TopBar_leftText);
(3)绑定完后调用方法ta.recycle();目的一是为了避免浪费资源,二是为了避免由于缓存引起的一些错误。
三.处理我们所需要的控件
leftButton = new Button(context);
rightButton = new Button(context);
title = new TextView(context);
leftButton.setText(leftText);
leftButton.setBackground(leftBackbound);
leftButton.setTextColor(leftTextColor);
四.将控件放到layout里面
(1)声明LayoutParams变量
private LayoutParams leftParams,rightParams,titleParams;
(2) 添加控件及其规则
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftParams);
....//余下省略
五.引用自定义view
修改activity_main.xml中的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:cwh= "http://schemas.android.com/apk/res/com.example.topbar"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.topbar.MainActivity"
tools:ignore="MergeRootFrame" >
<com.example.topbar.Topbar
android:id="@+id/topbar"
android:layout_width="wrap_content"
android:layout_height="40dp"
cwh:leftBackground = "@drawable/Square"
cwh:leftText = "Back"
cwh:leftTextColor = "#FFFFFF"
cwh:rightBackground = "@drawable/Square"
cwh:rightText = "More"
cwh:rightTextColor = "#FFFFFF"
cwh:title = "自定义标题"
cwh:titleTextColor = "#123412"
cwh:titleTextSize = "10sp">
</com.example.topbar.Topbar>
</RelativeLayout>
六.动态控制topbar
回调机制动态调用
private topbarClickListener listener;
public interface topbarClickListener{
public void leftClick();
public void rightClick();
}
public void setOnTopbarClickListener(topbarClickListener listener){
this.listener = listener;
}
设置按钮的监听事件
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
最后在MainAcitivity引用就可以动态控制了
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() {
// TODO Auto-generated method stub
}
@Override
public void leftClick() {
// TODO Auto-generated method stub
}
});
}