关闭

公共的标题栏构造器

122人阅读 评论(0) 收藏 举报
分类:

标题栏布局文件,一般包含三部分,左边、中间和右边,左右两边可能是文字,也可能是图片,中间部分是文字。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/common_titlebar"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/white"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >
    
    <RelativeLayout
        android:id="@+id/common_titlebar_iv_left_layout"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
         >

        <ImageView
           android:id="@+id/common_titlebar_iv_left"
           android:layout_width="30dp"
           android:layout_height="30dp"
           android:src="@drawable/back"
           android:layout_centerInParent="true" />
    </RelativeLayout>

    <TextView
        android:id="@+id/common_titlebar_tv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textColor="@color/username_textcolor"
        android:textSize="16sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/common_titlebar_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:singleLine="true"
        android:ellipsize="end"
        android:gravity="center"
        android:textColor="@color/username_textcolor"
        android:visibility="gone"
        android:textSize="18sp" />
    
    <RelativeLayout
        android:id="@+id/common_titlebar_iv_right_layout"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/common_titlebar_iv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>


    <TextView
        android:id="@+id/common_titlebar_tv_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textColor="@color/username_textcolor"
        android:textSize="16sp"
        android:visibility="gone" />

    <View
        android:id="@+id/common_titlebar_divider"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_alignParentBottom="true"
        android:background="@color/line" />

</RelativeLayout>

Builder设计模式


public class TitleBuilder {

	private View viewTitle;
	private TextView tvTitle;
	private ImageView ivLeft;
	private ImageView ivRight;
	private TextView tvLeft;
	private TextView tvRight;
	private RelativeLayout layoutLeft;
	private RelativeLayout layoutRight;
	private View divider;
	
	private void findView() {
		tvTitle = (TextView) viewTitle.findViewById(R.id.common_titlebar_tv);
		ivLeft = (ImageView) viewTitle.findViewById(R.id.common_titlebar_iv_left);
		ivRight = (ImageView) viewTitle.findViewById(R.id.common_titlebar_iv_right);
		tvLeft = (TextView) viewTitle.findViewById(R.id.common_titlebar_tv_left);
		tvRight = (TextView) viewTitle.findViewById(R.id.common_titlebar_tv_right);
		layoutLeft = (RelativeLayout) viewTitle.findViewById(R.id.common_titlebar_iv_left_layout);
		layoutRight = (RelativeLayout) viewTitle.findViewById(R.id.common_titlebar_iv_right_layout);
		divider = viewTitle.findViewById(R.id.common_titlebar_divider);
	}
	
	public TitleBuilder(Activity context) {
		viewTitle = context.findViewById(R.id.common_titlebar);
		findView();
	}

	public TitleBuilder(View context) {
		viewTitle = context.findViewById(R.id.common_titlebar);
		findView();
	}

	// 设置title背景
	public TitleBuilder setTitleBgRes(int resid) {
		viewTitle.setBackgroundResource(resid);
		return this;
	}

	// 设置title文字
	public TitleBuilder setTitleText(String text) {
		tvTitle.setVisibility(TextUtils.isEmpty(text) ? View.GONE
				: View.VISIBLE);
		tvTitle.setText(text);
		return this;
	}
	
	// 设置title文字颜色
	public TitleBuilder setTitleTextColor(int resid) {
		tvTitle.setTextColor(resid);
		return this;
	}

	//设置left图片资源
	public TitleBuilder setLeftImage(int resId) {
		layoutLeft.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
		ivLeft.setImageResource(resId);
		return this;
	}

	//设置left文字--如果文字不为空则将左边的图片隐藏
	public TitleBuilder setLeftText(String text) {
		tvLeft.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
		tvLeft.setText(text);
		layoutLeft.setVisibility(TextUtils.isEmpty(text) ? View.VISIBLE : View.GONE);
		return this;
	}
	
	//左边谁显示给谁设置监听器
	public TitleBuilder setLeftOnClickListener(OnClickListener listener) {
		if (layoutLeft.getVisibility() == View.VISIBLE) {
			layoutLeft.setOnClickListener(listener);
		} else if (tvLeft.getVisibility() == View.VISIBLE) {
			tvLeft.setOnClickListener(listener);
		}
		return this;
	}

	//设置right图片资源
	public TitleBuilder setRightImage(int resId) {
		layoutRight.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
		ivRight.setImageResource(resId);
		return this;
	}

	//设置right文字
	public TitleBuilder setRightText(String text) {
		tvRight.setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE);
		tvRight.setText(text);
		return this;
	}
	
	//设置分割线是否显示
	public TitleBuilder setDividerVisibility(int visibility) {
		divider.setVisibility(visibility);
		return this;
	}
	
	//右边谁显示给谁设置监听器
	public TitleBuilder setRightOnClickListener(OnClickListener listener) {
		if (layoutRight.getVisibility() == View.VISIBLE) {
			layoutRight.setOnClickListener(listener);
		} else if (tvRight.getVisibility() == View.VISIBLE) {
			tvRight.setOnClickListener(listener);
		}
		return this;
	}

	public View build() {
		return viewTitle;
	}

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:104240次
    • 积分:1925
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:18篇
    • 译文:0篇
    • 评论:8条
    文章分类
    最新评论