packagecom.example.zhengcheng.myapplication;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.drawable.Drawable;importandroid.util.AttributeSet;importandroid.view.Gravity;importandroid.view.View;importandroid.widget.Button;importandroid.widget.RelativeLayout;importandroid.widget.TextView;/*** Created by zhengcheng on 2015/4/11.*/
public class TopBar extendsRelativeLayout {privateButton btn_left, btn_right;privateTextView tv_title;private intleftTextColor;privateDrawable leftBackground;privateString leftText;private intrightTextColor;privateDrawable rightBackground;privateString rightText;private floattitleTextSize;private inttitleTextColor;privateString toptitle;//定义三个布局参数
privateLayoutParams leftParams, rightParams, titleParams;//定义一个事件接口
public interfacetopbarClickListener{public voidleftClick();public voidrightClick();
}//创建接口对象
publictopbarClickListener listener;//创建为事件接口赋值的方法
public voidsetOnTopBarClickListener(topbarClickListener listener){this.listener =listener;
}//构造方法,初始化成员
publicTopBar(Context context, AttributeSet attrs) {super(context, attrs);//将XML中定义的自定义属性映射到attrs中。
TypedArray ta =context.obtainStyledAttributes(attrs, R.styleable.Topbar);//从ta结构中获取数据,类似一种key,value结构,通过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);
toptitle=ta.getString(R.styleable.Topbar_toptitle);//进行垃圾回收
ta.recycle();//初始化控件
btn_left = newButton(context);
btn_right= newButton(context);
tv_title= newTextView(context);//设置控件的值
btn_left.setTextColor(leftTextColor); //设置文字颜色
btn_left.setBackground(leftBackground); //设置背景
btn_left.setText(leftText); //设置文本
btn_right.setTextColor(rightTextColor);//设置文字颜色
btn_right.setBackground(rightBackground); //设置背景
btn_right.setText(rightText); //设置文本
tv_title.setTextColor(titleTextColor);//设置字体颜色
tv_title.setTextSize(titleTextSize); //设置字体大小
tv_title.setText(toptitle); //设置文本
tv_title.setGravity(Gravity.CENTER); //居中显示
setBackgroundColor(0xfff59563); //设置View的背景颜色//设置布局属性的width和height
leftParams = newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//设置对齐方式为父容器的左侧
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);//将左边按钮添加到视图中,并设置布局属性
addView(btn_left, leftParams);//设置布局属性的width和height
rightParams = newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//设置对齐方式为父容器的右侧
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);//将右边按钮添加到视图中,并设置布局属性
addView(btn_right, rightParams);//设置布局属性的width和height
titleParams = newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);//设置对齐方式为居中对齐
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);//将中间TextView添加到视图中,并设置布局属性
addView(tv_title, titleParams);//添加左侧按钮的Click事件
btn_left.setOnClickListener(newOnClickListener() {
@Overridepublic voidonClick(View v) {
listener.leftClick();
}
});//添加右侧按钮的Click事件
btn_right.setOnClickListener(newOnClickListener() {
@Overridepublic voidonClick(View v) {
listener.rightClick();
}
});
}/*** 设置左边按钮是否隐藏,true隐藏, false消失
*@paramflag*/
public void setLeftButtonIsVisiable(booleanflag){if(flag){
btn_left.setVisibility(View.VISIBLE);
}else{
btn_left.setVisibility(View.GONE);
}
}/*** 设置右边按钮是否隐藏,true隐藏, false消失
*@paramflag*/
public void setRightButtonIsVisiable(booleanflag){if(flag){
btn_right.setVisibility(View.VISIBLE);
}else{
btn_right.setVisibility(View.GONE);
}
}
}