必须先看
复合控件:
1.可以将几个控件组合成一个新的控件,在需要时调用即可,例如常见的TitleBar2.可以复用一个TitleBar,左右显示图标或者标题,设置颜色,设置大小,
实现步骤:
1.先创建一个TitleBar布局.xml(例如一个一个相对布局-----里面包含5个控件,其中左边一个图片按钮,一个文本.中间一个文本,右边一个图片按钮,右边一个文本)
2..创建一个类MyTitleBar.java,继承FrameLayout/RelativeLayout/LinearLayout其中一个布局,在构造函数中将MyTitleBar布局实例化成View对象
3.在values目录下,创建一个attrs.xml,声明属性标签,可以用TypedArray对象获取这些属性的值
4.在1.2步骤中写入如下
reference:资源ID
color:颜色值
boolean:布尔值
dimension:尺寸值
float:浮点值
integer:整型值
string:字符串
fraction:百分数
enum:枚举值
flag:位或运算
可以定义多种,例如:reference|string
//1.创建一个类,继承FrameLayout/RelativeLayout/LinearLayout其中一个布局
public class MyTittleBar extends FrameLayout implements OnClickListener {
private Activity activity;
private View tittleBar;// 2.2自定义TittleBar..做成全局变量
private ImageView left_img;// 2.3左边图片按钮
private ImageView right_img;// 2.3右边图片按钮
private TextView left_txt;// 2.3左边文本
private TextView right_txt;// 2.3右边文本
private TextView title_txt;// 2.3中间标题文本
private String left_txt_string;// 3.2设置左边文本值
private String right_txt_string;
private String title_txt_string;
private Boolean isShow_left_img;// 3.2是否显示左边图片
private Boolean isShow_right_img;
private Boolean isShow_left_txt;// 3.2是否显示文本
private Boolean isShow_right_txt;
private float left_txt_size;// 3.2设置左边文本大小
private float right_txt_size;
private float title_txt_size;
private int left_txt_color;// 3.2设置左边文本颜色
private int right_txt_color;
private int title_txt_color;
public void setTitle_txt_color(int title_txt_color) {
this.title_txt_color = title_txt_color;
title_txt.setTextColor(title_txt_color);
}
private int bg;// 3.2设置整个布局背景
public interface OnLeftTxtClickListener {// 4.1文本实现监听
public void onLeftTxtClick(View v);
}
public interface OnRightTxtClickListener {// 4.1文本实现监听
public void onRightTxtClick(View v);
}
public interface OnLeftImgClickListener {// 4.1图片实现监听
public void onLeftImgClick(View v);
}
public interface OnRightImgClickListener {// 4.1图片实现监听
public void onRightImgClick(View v);
}
private OnLeftTxtClickListener onLeftTxtClickListener;// 4.2监听接口
private OnRightTxtClickListener onRightTxtClickListener;// 4.2监听接口
private OnLeftImgClickListener onLeftImgClickListener;// 4.2监听接口
private OnRightImgClickListener onRightImgClickListener;// 4.2监听接口
public void setOnLeftTxtClickListener(OnLeftTxtClickListener onLeftTxtClickListener) {
this.onLeftTxtClickListener = onLeftTxtClickListener;
}
public void setOnRightTxtClickListener(OnRightTxtClickListener onRightTxtClickListener) {
this.onRightTxtClickListener = onRightTxtClickListener;
}
public void setOnLeftImgClickListener(OnLeftImgClickListener onLeftImgClickListener) {
this.onLeftImgClickListener = onLeftImgClickListener;
}
public void setOnRightImgClickListener(OnRightImgClickListener onRightImgClickListener) {
this.onRightImgClickListener = onRightImgClickListener;
}
public MyTittleBar(Context context) {// 1.1构造函数,用与代码new出对象
super(context);
InitView(context);// 2.1调用方法
}
public MyTittleBar(Context context, AttributeSet attrs) {// 1.2构造函数,用与xml文件布局
super(context, attrs);
LogUtils.e(""+title_txt_color);
InitView(context);// 2.1调用方法
LogUtils.e(""+title_txt_color);
InitXMLValues(context, attrs);// 3.3调用方法
LogUtils.e(""+title_txt_color);
InitListtener();// 5.1调用方法
LogUtils.e(""+title_txt_color);
}
private void InitView(Context context) {// 2.初始化View布局
activity = (Activity) context;// 获取当前TitleBar所在的Activity
tittleBar = View.inflate(context, R.layout.widget_action_bar, null);// 找出布局
left_img = (ImageView) tittleBar.findViewById(R.id.left_img);
right_img = (ImageView) tittleBar.findViewById(R.id.right_img);
left_txt = (TextView) tittleBar.findViewById(R.id.left_txt);
right_txt = (TextView) tittleBar.findViewById(R.id.right_txt);
title_txt = (TextView) tittleBar.findViewById(R.id.title_txt);
this.addView(tittleBar, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));// 2.把所有布局增加到该布局
}
private void InitXMLValues(Context context, AttributeSet attrs) {// 3.根据values资源文件中定义好的。。。arrs.xml属性,初始化并且可以在xml配置相关属性
TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.mytitlebar);// 3.1这个类,能获取到arrs.xml.自己定义好的一些属性
left_txt_string = type.getString(R.styleable.mytitlebar_left_txt);// 3.2设置文本值
right_txt_string = type.getString(R.styleable.mytitlebar_right_txt);//
title_txt_string = type.getString(R.styleable.mytitlebar_title_txt);//
isShow_left_img = type.getBoolean(R.styleable.mytitlebar_left_img, false);// 3.2是否显示图片
isShow_right_img = type.getBoolean(R.styleable.mytitlebar_right_img, false);
isShow_left_txt = type.getBoolean(R.styleable.mytitlebar_left_txt_see, false);// 3.2是否显示文本
isShow_right_txt = type.getBoolean(R.styleable.mytitlebar_right_txt_see, false);
left_txt_size = type.getDimensionPixelSize(R.styleable.mytitlebar_left_txt_size, 12);// 3.2设置文本大小
right_txt_size = type.getDimensionPixelSize(R.styleable.mytitlebar_right_txt_szie, 12);
title_txt_size = type.getDimensionPixelSize(R.styleable.mytitlebar_title_txt_size, 12);
left_txt_color = type.getResourceId(R.styleable.mytitlebar_left_txt_color, R.color.white);// 3.2设置文颜色
right_txt_color = type.getResourceId(R.styleable.mytitlebar_right_txt_color, R.color.white);
title_txt_color = type.getResourceId(R.styleable.mytitlebar_tittle_txt_color, R.color.white);
bg = type.getInt(R.styleable.mytitlebar_bg, R.drawable.ic_launcher);// 3.2设置背景
type.recycle();
}
private void InitListtener() {// 5.监听
if (isShow_left_img) {
if (left_img != null) {
left_img.setVisibility(View.VISIBLE);
left_img.setOnClickListener(this);// 4.本类实现监听
}
} else {
left_img.setVisibility(View.INVISIBLE);
}
if (isShow_right_img) {
if (right_img != null) {
right_img.setVisibility(View.VISIBLE);
right_img.setOnClickListener(this);// 4.本类实现监听
}
} else {
right_img.setVisibility(View.INVISIBLE);
}
if (isShow_left_txt) {
if (left_txt != null) {
left_txt.setVisibility(View.VISIBLE);
left_txt.setOnClickListener(this);// 4.本类实现监听
left_txt.setText(left_txt_string);
}
} else {
left_txt.setVisibility(View.INVISIBLE);
}
if (isShow_right_txt) {
if (right_txt != null) {
right_txt.setVisibility(View.VISIBLE);
right_txt.setOnClickListener(this);// 4.本类实现监听
right_txt.setText(right_txt_string);
}
} else {
right_txt.setVisibility(View.INVISIBLE);
}
if (title_txt != null) {
title_txt.setText(title_txt_string);
title_txt.setTextColor(getResources().getColor(title_txt_color));
title_txt.setTextSize(title_txt_size);
}
}
@Override
public void onClick(View v) {// 4.3提供外部监听
switch (v.getId()) {
case R.id.left_txt:
if (onLeftTxtClickListener != null) {
onLeftTxtClickListener.onLeftTxtClick(v);
}
break;
case R.id.right_txt:
if (onRightTxtClickListener != null) {
onRightTxtClickListener.onRightTxtClick(v);
}
break;
case R.id.left_img:
activity.finish();
break;
case R.id.right_img:
if (onRightImgClickListener != null) {
onRightImgClickListener.onRightImgClick(v);
}
break;
}
}
}