然后我们再重新写一个类继承于LineaLayour。通过在构造方法中去加载这个布局文件,这样就能实现一个组合控件的定义了,这样的方式是不是特别简单。
public class UINavigationView2 extends LinearLayout {
private Button btn_left;
private Button btn_right;
private TextView tv_title;
public UINavigationView2(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public UINavigationView2(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.title_bar, null);
btn_left = (Button) findViewById(R.id.btn_left);
btn_right = (Button) findViewById(R.id.btn_right);
tv_title = (Button) findViewById(R.id.tv_title);
}
public void setBtnLeftBacground(int resId) {
if (btn_left != null) {
btn_left.setBackgroundResource(resId);
}
}
public void setBtnRightBacground(int resId) {
if (btn_right != null) {
btn_right.setBackgroundResource(resId);
}
}
public void setTvTitle(int resId) {
if (tv_title != null) {
tv_title.setText(resId);
}
}
public Button getBtn_left() {
return btn_left;
}
public Button getBtn_right() {
return btn_right;
}
public TextView getTv_title() {
return tv_title;
}
}