android 自定义标题栏


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


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

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

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

     
        
        
        
        
        
        
        
        
        
        
        
        
        
    

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

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

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);
	}
}

最后在布局文件的使用:


    

就这样了  到此为止!



你可能感兴趣的:(自定义控件)