android 自定义标题栏


在写代码时,我们尽量做到后期维护减少代码的工作,比如一个需求过来,我们不可能在以前代码的基础上做很大的修改,所以在前期就要我们想清楚,怎么做到扩展性比较好,我们在做app的时候,标题栏是每个都会有的,比如下面这张图


这是很常用的标题栏,一般的做法就是在每个布局中都写一遍,这样肯定后期维护不好,比如要换个图片,或者是字体大小,颜色等,这要有面向对象的封装,有点像吧,所以我们最好是能统一,后期维护起来就非常简单了,今天就做一个自定义的标题栏,新建一个android项目 名称为commontitle

自定义一个CustomTitleBar继承自RelativeLayout,我们自己编写一个布局,然后自定义一些属性,用来控制这个控件是否显示,以及标题栏的文字信息等,

定义一个CustomTitleBar继承自RelativeLayout,在value/attrs文件下定义一下属性

     <declare-styleable name="CustomTitleBar">
        <attr name="left_button_visible" format="boolean"></attr>
        <attr name="right_button_visible" format="boolean"></attr>
        <attr name="title_text" format="string"></attr>
        <attr name="title_text_color" format="integer"></attr>
        <attr name="title_text_background" format="reference|integer"></attr>
        <attr name="title_text_drawable" format="reference|integer"></attr>
        <attr name="right_button_text" format="string"></attr>
        <attr name="right_button_text_color" format="integer"></attr>
        <attr name="right_button_drawable" format="reference|integer"></attr>
        <attr name="left_button_text" format="string"></attr>
        <attr name="left_button_text_color" format="integer"></attr>
        <attr name="left_button_drawable" format="reference|integer"></attr>
        <attr name="line_visible" format="boolean"></attr>
    </declare-styleable>

从这些属性的名字就可以看出什么意思,

贴出整个自定义类,没啥难度

package com.example.commontitle.ui;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.commontitle.R;
@SuppressLint("Recycle")
public class CustomTitleBar extends RelativeLayout {
	private Button title_bar_left;//左边的返回键
	private Button title_bar_right;//右边的文字信息
	private TextView title_bar_title;//标题栏
	private View view_line;//标题栏下面的线
	public CustomTitleBar(Context context, AttributeSet attrs,
			int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
	}

	public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public CustomTitleBar(final Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = LayoutInflater.from(context).inflate(R.layout.custom_title_bar, this, true);
		title_bar_left = (Button) view.findViewById(R.id.title_bar_left);
		title_bar_right = (Button) view.findViewById(R.id.title_bar_right);
		title_bar_title = (TextView) view.findViewById(R.id.title_bar_title);
		view_line = view.findViewById(R.id.view_line);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
		if(typedArray!=null){
			//判断左边控件是否显示
			boolean left_button_visible = typedArray.getBoolean(R.styleable.CustomTitleBar_left_button_visible, false);
			if(left_button_visible){
				title_bar_left.setVisibility(View.GONE);
			}else{
				title_bar_left.setVisibility(View.VISIBLE);
			}
			String left_text = typedArray.getString(R.styleable.CustomTitleBar_left_button_text);
			if(!TextUtils.isEmpty(left_text)){
				title_bar_left.setText(left_text);
			}else {
				int leftButtonDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.drawable.go_back);
				if (leftButtonDrawable != -1) {
					title_bar_left.setBackgroundResource(leftButtonDrawable);
				}
			}
			//标题栏文字
			String title = typedArray.getString(R.styleable.CustomTitleBar_title_text);
			title_bar_title.setText(title);
			//判断右边
			boolean rightButtonVisible = typedArray.getBoolean(R.styleable.CustomTitleBar_right_button_visible, false);
			if(rightButtonVisible){
				title_bar_right.setVisibility(View.GONE);
			}else{
				title_bar_right.setVisibility(View.VISIBLE);
			}
			String right_text = typedArray.getString(R.styleable.CustomTitleBar_right_button_text);
			title_bar_right.setText(right_text);
			typedArray.recycle();
		}
		if(title_bar_left!=null){
			//左边返回键按钮
			title_bar_left.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					((Activity)context).finish();
				}
			});
		}
	}

	public CustomTitleBar(Context context) {
		super(context);
	}
}

最后在布局文件的使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:eb="http://schemas.android.com/apk/res/com.example.commontitle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <com.example.commontitle.ui.CustomTitleBar
        android:id="@+id/common_titlebar"
        android:layout_width="match_parent"
        android:layout_height="45dip"
        android:background="#31DFDF"
        eb:left_button_text=""
        eb:left_button_visible="false"
        eb:line_visible="false"
        eb:right_button_visible="false"
        eb:right_button_text="编辑"
        eb:title_text_color="@color/white"
        eb:title_text="选择银行卡"
         />
</RelativeLayout>

就这样了  到此为止!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值