布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/title_bar_color"
android:gravity="center_vertical"
android:paddingTop="20dp">
<ImageView
android:id="@+id/title_ib_back"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:scaleType="centerInside"
android:src="@drawable/bg_back_selector" />
<TextView
android:id="@+id/title_txt_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="5dp"
android:background="@null"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/font_size_14sp" />
<TextView
android:id="@+id/title_txt_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:maxEms="15"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="@dimen/font_size_18sp" />
<TextView
android:id="@+id/title_txt_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="5dp"
android:textColor="@color/white"
android:textSize="@dimen/font_size_14sp" />
<ImageView
android:id="@+id/title_ib_right"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:scaleType="centerInside" />
</RelativeLayout>
attrs属性
<!-- titlebar 标题栏 -->
<declare-styleable name="TitleView">
<attr name="textInfoLeft" format="string"></attr>
<attr name="textInfoCenter" format="string"></attr>
<attr name="textInfoRight" format="string"></attr>
<attr name="leftBtnIcon" format="reference"></attr>
<attr name="backBtnIcon" format="reference"></attr>
<attr name="rightBtnIcon" format="reference"></attr>
<attr name="rightBtnIcon2" format="reference"></attr>
<attr name="rightBtnIcon3" format="reference"></attr>
<attr name="hasBackBtn" format="boolean"></attr>
<attr name="hasLeftBtn" format="boolean"></attr>
<attr name="hasRightBtn" format="boolean"></attr>
<attr name="hasRightBtn2" format="boolean"></attr>
<attr name="hasRightBtn3" format="boolean"></attr>
</declare-styleable>
最后是核心代码
package com.example.administrator.newspolice.view;
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.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.administrator.newspolice.R;
/**
* Description:标题布局
*/
public class TitleView extends RelativeLayout {
/**
* 文本信息
*/
private String textInfoLeft;
private String textInfoCenter;
private String textInfoRight;
/**
* 是否有返回按钮、右侧按钮
*/
private boolean hasBackBtn;
private boolean hasRightBtn;
/**
* 标题
*/
private TextView txtLeft;
private TextView txtCenter;
private TextView txtRight;
/**
* 返回按钮、右侧按钮
*/
private ImageView ibBack;
private ImageView ibRight;
public TitleView(Context context) {
this(context, null);
}
public TitleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.title_view_layout, this);
ibBack = (ImageView) findViewById(R.id.title_ib_back);
ibRight = (ImageView) findViewById(R.id.title_ib_right);
txtLeft = (TextView) findViewById(R.id.title_txt_left);
txtCenter = (TextView) findViewById(R.id.title_txt_center);
txtRight = (TextView) findViewById(R.id.title_txt_right);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
int n = array.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = array.getIndex(i);
switch (attr) {
case R.styleable.TitleView_textInfoLeft:
textInfoLeft = array.getString(attr);
break;
case R.styleable.TitleView_textInfoCenter:
textInfoCenter = array.getString(attr);
break;
case R.styleable.TitleView_textInfoRight:
textInfoRight = array.getString(attr);
break;
// 是否有左侧按钮,默认设置为false
case R.styleable.TitleView_hasBackBtn:
hasBackBtn = array.getBoolean(attr, false);
break;
// 是否有右侧按钮,默认设置为false
case R.styleable.TitleView_hasRightBtn:
hasRightBtn = array.getBoolean(attr, false);
break;
// 左边侧按钮图片
case R.styleable.TitleView_leftBtnIcon:
ibBack.setImageResource(array.getResourceId(attr, -1));
break;
// 右侧按钮图片
case R.styleable.TitleView_rightBtnIcon:
ibRight.setImageResource(array.getResourceId(attr, -1));
break;
}
}
array.recycle();
if (!TextUtils.isEmpty(textInfoLeft)) {
txtLeft.setText(textInfoLeft);
}
if (!TextUtils.isEmpty(textInfoCenter)) {
txtCenter.setText(textInfoCenter);
}
if (!TextUtils.isEmpty(textInfoRight)) {
txtRight.setText(textInfoRight);
}
if (!hasBackBtn) {
ibBack.setVisibility(View.GONE);
} else {
ibBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
}
if (!hasRightBtn) {
ibRight.setVisibility(View.GONE);
}
}
public void setHasRightBtn(boolean tag) {
if(tag){
ibRight.setVisibility(View.VISIBLE);
}else{
ibRight.setVisibility(View.GONE);
}
}
public void setHasRightInfo(boolean tag) {
if(tag){
txtRight.setVisibility(View.VISIBLE);
}else{
txtRight.setVisibility(View.GONE);
}
}
public void setTextInfoLeft(String textInfoLeft) {
txtLeft.setText(textInfoLeft);
}
public void setTextInfoCenter(String textInfoCenter) {
txtCenter.setText(textInfoCenter);
}
public void setTextInfoRight(String textInfoRight) {
txtRight.setText(textInfoRight);
}
public void setRightBtnIcon(int resId) {
ibRight.setImageResource(resId);
}
public void setRightTxtBg(int resId) {
txtRight.setBackgroundResource(resId);
}
public void setRightTxtColor(int resId) {
txtRight.setTextColor(resId);
}
public void setLeftBtnListener(OnClickListener l) {
ibBack.setOnClickListener(l);
}
public void setRightBtnListener(OnClickListener l) {
ibRight.setOnClickListener(l);
}
public void setLeftTextInfoListener(OnClickListener l) {
txtLeft.setOnClickListener(l);
}
public void setRightTextInfoListener(OnClickListener l) {
txtRight.setOnClickListener(l);
}
}