您的位置:首页 > 其它

自定义标签 titlebar

2015-10-23 13:38 295 查看
开发前期,titlebar demo案例

实现步骤

1在values下新建attrs.xml定义组件属性

    <declare-styleable name="TitleBar">

        <attr name="backgroundColor" format="color"/>

        <attr name="titleText" format="string"/>

        <attr name="titleTextSize" format="dimension"/>

        <attr name="titleTextColor" format="color"/>

        

        <attr name="leftText" format="string"/>

        <attr name="leftBackground" format="dimension"/>

        <attr name="leftTextSize" format="dimension"/>

        <attr name="marginLeft" format="dimension"/>

        <attr name="leftIsVisibility" format="boolean"/>

        

        <attr name="rightText" format="string"/>

        <attr name="rightBackground" format="dimension"/>

        <attr name="rightTextSize" format="dimension"/>

        <attr name="rightTextColor" format="color"/>

        <attr name="marginRight" format="dimension"/>

        <attr name="rightIsVisibility" format="boolean"/>

    </declare-styleable>

2.组装布局

public class TitleBar extends RelativeLayout {

    private int backgroundColor; //默认背景

    private Button lButton,rButton;//左右按钮

    private TextView textView;//title

    private float lTextSize;//左边按钮文字大小

    private int lBackground;//左按钮背景

    private String lText;//左按钮文字

    private float rTextSize; //右按钮文字大小

    private int rBackground;//右按钮背景

    private String rText;//右按钮文字

    private int rightTextColor;//右按钮文字颜色

    

    private float titleTextSize;

    private int titleTextColor;

    private String title;

    private float marginLeft,marginRight;

    private RelativeLayout.LayoutParams lparams,rparams,tvparams;

    private TitleBarOnclick onClickListener;

    public TitleBar(Context context) {

        super(context);

    }

    public interface TitleBarOnclick{

        public void LeftButtonOnclick();

        public void RightButtonOnclick();

    }

    public void setOnClickListener(TitleBarOnclick onClickListener){

        this.onClickListener = onClickListener;

    }

    public TitleBar(Context context, AttributeSet attrs) {

        super(context, attrs);

        //获取配置数组

        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TitleBar);

        

        //获取背景色

        backgroundColor = array.getColor(R.styleable.TitleBar_backgroundColor, Color.BLACK);

        lTextSize = array.getDimension(R.styleable.TitleBar_leftTextSize,PXDP.dip2px(getContext(), 12));

        lBackground = array.getResourceId(R.styleable.TitleBar_leftBackground,0);

        lText = array.getString(R.styleable.TitleBar_leftText);

        marginLeft = array.getDimension(R.styleable.TitleBar_marginLeft,PXDP.dip2px(getContext(), 8));

        rTextSize = array.getDimension(R.styleable.TitleBar_rightTextSize,PXDP.dip2px(getContext(), 12));

        rBackground = array.getResourceId(R.styleable.TitleBar_rightBackground,0);

        rText = array.getString(R.styleable.TitleBar_rightText);

        rightTextColor = array.getColor(R.styleable.TitleBar_rightTextColor,Color.BLACK);

        marginRight = array.getDimension(R.styleable.TitleBar_marginRight,PXDP.dip2px(getContext(), 8));

        titleTextSize = array.getDimension(R.styleable.TitleBar_titleTextSize,PXDP.dip2px(getContext(),18));

        titleTextColor = array.getColor(R.styleable.TitleBar_titleTextColor, Color.BLACK);

        title = array.getString(R.styleable.TitleBar_titleText);

        boolean rightVesible = array.getBoolean(R.styleable.TitleBar_rightIsVisibility,true);

        boolean leftVesible = array.getBoolean(R.styleable.TitleBar_leftIsVisibility,true);

        

        array.recycle();

        //设置背景色

        setBackgroundColor(backgroundColor);

        //创建左按钮

        lButton = new Button(context);

        lButton.setText(lText);

        lButton.setTextSize(TypedValue.COMPLEX_UNIT_PX,lTextSize);

        lButton.setGravity(Gravity.CENTER);

        lButton.setPadding(PXDP.dip2px(getContext(), 8), 0, PXDP.dip2px(getContext(), 8), 0);

        lButton.setBackgroundResource(lBackground);

        //创建右按钮

        rButton = new Button(context);

    

        rButton.setText(rText);

        rButton.setGravity(Gravity.CENTER);

        rButton.setTextSize(TypedValue.COMPLEX_UNIT_PX,rTextSize);

        rButton.setPadding(PXDP.dip2px(getContext(), 8), PXDP.dip2px(getContext(), 3), PXDP.dip2px(getContext(), 8), 0);

        rButton.setTextColor(rightTextColor);
rButton.setBackgroundResource(rBackground);

        //创建title标题

        textView = new TextView(context);

        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleTextSize);

        textView.setTextColor(titleTextColor);

        textView.setText(title);

        textView.setGravity(Gravity.CENTER);

        //添加左按钮

        lparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);

        lparams.setMargins((int)marginLeft, 0, 0, 0);

        lparams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);

        addView(lButton, lparams);

        //添加右按钮

        rparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);

        rparams.setMargins(0, 0,(int)marginRight, 0);

        rparams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);

        addView(rButton, rparams);

        //添加title文本

        tvparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);

        tvparams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);

        addView(textView, tvparams);

        

        

        setRightVisibility(rightVesible);

        setLeftVisibility(leftVesible);

        lButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                onClickListener.LeftButtonOnclick();

            }

        });

        rButton.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                onClickListener.RightButtonOnclick();

            }

        });

    }

    

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

    }

    

    /**

     * @Title: setRightVisibility

     * @Description: 右侧按钮是否显示     true 显示     false 不显示

     * @param visible

     * @return: void

     */

    public void setRightVisibility(boolean visible){

    if (visible) {

    rButton.setVisibility(View.VISIBLE);
}else{
rButton.setVisibility(View.GONE);
}

    }

    /**

     * @Title: setLeftVisibility

     * @Description: 右侧按钮是否显示     true 显示     false 不显示

     * @param visible

     * @return: void

     */

    public void setLeftVisibility(boolean visible){

    if (visible) {

    lButton.setVisibility(View.VISIBLE);
}else{
lButton.setVisibility(View.GONE);
}

    }
}

3.引用

<com.mobile.aqtech.utils.view.TitleBar

        android:layout_width="match_parent"

        android:layout_height="38dp"

        android:layout_marginTop="10dp"

        app:backgroundColor="@color/purple"

        app:leftBackground="@drawable/title_bar_test"

        app:leftIsVisibility="true"

        app:rightBackground="@null"

        app:rightIsVisibility="true"

        app:rightText="更多功能"

        app:rightTextSize="12dp"

        app:titleText="@string/menu_title_show"

        app:titleTextSize="18dp" >

    </com.mobile.aqtech.utils.view.TitleBar>

测试图片:



源码链接:http://download.csdn.net/detail/clare_ju/9115201
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息