TitleBar

来源:互联网 发布:淘宝 网页版 编辑:程序博客网 时间:2024/05/17 00:06

目前许多app的页面都是采用titlebar+content的组合方式,如下图所示

360截图-35517418

其中TitleBar可以提取出来作为一个模块在多个项目里面进行复用。

以下是我自己写的TitleBar,其最终效果如图所示:

 

360截图-35650284

如果不清楚自定义View的创建步骤,请参见我之前的博客,自定义控件

TitleBar由三部分组成,左边是Button,中间是TextView,右边是Button,并设置了如下的自定义属性

<declare-styleable name="TitleBar">    <attr name="title" format="string"/>    <attr name="titleTextSize" format="dimension"/>    <attr name="titleTextColor" format="color"/>    <attr name="leftTextColor" format="color"/>    <attr name="leftText" format="string|reference"/>    <attr name="leftTextSize" format="reference|dimension"/>    <attr name="rightTextColor" format="color"/>    <attr name="rightText" format="string|reference"/>    <attr name="rightTextSize" format="dimension|reference"/></declare-styleable>

并实现了TitleBarI接口,接口如下

public interface TitleBarI {    public void setTitle(String title);    //设置左边的按钮为返回键    public void setLeftBack();    public void setLeftAction(View.OnClickListener onClickListener);    //设置左边按钮是否可见,true可见,false隐藏    public void setLeftVisible(boolean visible);    public void setRightAction(View.OnClickListener onClickListener);    public void setRightVisible(boolean visible);    public View getLeftButton();    public View getRightButton();}

其中TitleBar继承自RelativeLayout,并为其增加了三个View,

其中左边按钮默认为“”,中间默认为“标题”,右边的按钮默认为“···

具体如下

private void initView(Context context){    //设置left    left_btn = new Button(context);    if(leftStr==null) {        left_btn.setText("←");    }else{        left_btn.setText(leftStr);    }    left_btn.setBackgroundColor(Color.TRANSPARENT);    left_btn.setTextColor(leftColor);    left_btn.setTextSize(leftSize);    left_btn.setGravity(Gravity.CENTER);    leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,            LayoutParams.WRAP_CONTENT );    leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);    addView(left_btn,leftParams);    //设置title    title_txv = new TextView(context);    if(title==null) {        title_txv.setText("标题");    }else{        title_txv.setText(title);    }    title_txv.setTextColor(titleColor);    title_txv.setTextSize(titleSize);    title_txv.setBackgroundColor(Color.TRANSPARENT);    titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,            LayoutParams.WRAP_CONTENT);    titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);    addView(title_txv,titleParams);    //设置right    right_btn = new Button(context);    if(rightStr==null) {        right_btn.setText("•••");    }else{        right_btn.setText(rightStr);    }    right_btn.setTextSize(rightSize);    right_btn.setTextColor(rightColor);    right_btn.setBackgroundColor(Color.TRANSPARENT);    right_btn.setGravity(Gravity.CENTER);    rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,            LayoutParams.WRAP_CONTENT);    rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);    addView(right_btn,rightParams);}

源码的地址为:https://git.oschina.net/huangbei1990/Widgets.git

0 0