Android顶部标题栏--HeaderBarView

image.png

image.png

image.png

第一步:
在values下面新建attrs文件,设置属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!--足迹顶部标题栏自定义属性-->
  <declare-styleable name="HeaderBarView">
    <attr name="headerLeftText" format="string"/>
    <attr name="headerLeftTextColor" format="color"/>
    <attr name="headerLeftTextSize" format="dimension"/>

    <attr name="headerCenterTextColor" format="color"/>
    <attr name="headerCenterTitle" format="string"/>
    <attr name="headerCenterTextSize" format="dimension"/>

    <attr name="headerCenterText2Color" format="color"/>
    <attr name="headerCenterTitle2" format="string"/>
    <attr name="headerCenterText2Size" format="dimension"/>
    <attr name="headerCenterTitle2Visibility" format="string"/>
    <attr name="headerCenterTitle2DrawableLeft" format="reference"/>

    <attr name="headerRightTextColor" format="color"/>
    <attr name="headerRightText" format="string"/>
    <attr name="headerRightTextSize" format="dimension"/>

    <attr name="headerLeftDrawble" format="reference"/>
    <attr name="headerRightDrawable" format="reference"/>
    <attr name="headerCenterLeftDrawbleVisibility" format="string"/>
    <attr name="headerCenterRightDrawableVisibility" format="string"/>
  </declare-styleable>
</resources>

主要设置内容,颜色,图片等

第二步:

设置标题栏的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout_left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal"
        android:paddingLeft="@dimen/size_8"
        android:paddingRight="@dimen/size_8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/iv_left"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:contentDescription="@null"
            android:scaleType="centerInside"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/tv_left"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLength="4"
            android:maxLines="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/iv_left"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <LinearLayout
        android:id="@+id/layout_center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="@dimen/size_8"
        android:paddingRight="@dimen/size_8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLength="10"
            android:maxLines="1" />

        <TextView
            android:id="@+id/tv_title_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLength="10"
            android:maxLines="1" />
    </LinearLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingLeft="@dimen/size_8"
        android:paddingRight="@dimen/size_8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLines="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/iv_right"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/iv_right"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:contentDescription="@null"
            android:scaleType="centerInside"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv_right"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

这里的布局文件是通用的布局文件,需要对此布局根据自己的需求进行配置,这个就需要自定义View了

第三步:

自定义HeaderBarView,主要进行引用布局,初始化view,拿到自定义属性进行相应配置

    public HeaderBarView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.zuji_header_bar_view, this);
        initView();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.HeaderBarView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.HeaderBarView_headerLeftTextColor:
                    tv_left.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerLeftDrawble:
                    iv_left.setImageResource(array.getResourceId(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerLeftTextSize:
//                    tv_left.setTextSize(array.getDimension(attr,12f));
                    tv_left.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerLeftText:
                    tv_left.setText(array.getString(attr));
                    break;

                case R.styleable.HeaderBarView_headerCenterTextColor:
                    tv_title.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle:
                    tv_title.setText(array.getString(attr));
                    break;
                case R.styleable.HeaderBarView_headerCenterTextSize:
                    tv_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;

                case R.styleable.HeaderBarView_headerCenterText2Color:
                    tv_title2.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle2:
                    tv_title2.setText(array.getString(attr));
                    break;
                case R.styleable.HeaderBarView_headerCenterText2Size:
//                    tv_title2.setTextSize(array.getDimensionPixelSize(attr, 12));
//                    int size = array.getDimensionPixelSize(attr, 0);
                    tv_title2.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle2Visibility:
                    String visible = array.getString(attr);
                    setVisibility(tv_title2,visible);
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle2DrawableLeft:
                    Drawable drawableLeft = null;
                    drawableLeft = array.getDrawable(attr);
                    /// 这一步必须要做,否则不会显示.
                    drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
                    tv_title2.setCompoundDrawables(drawableLeft, null, null, null);
                    break;

                case R.styleable.HeaderBarView_headerRightDrawable:
                    iv_right.setImageResource(array.getResourceId(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerRightText:
                    tv_right.setText(array.getString(attr));
                    break;
                case R.styleable.HeaderBarView_headerRightTextColor:
                    tv_right.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerRightTextSize:
                    tv_right.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;

                case R.styleable.HeaderBarView_headerCenterLeftDrawbleVisibility:
                    String visible2 = array.getString(attr);
                    setVisibility(iv_left,visible2);
                    break;
                case R.styleable.HeaderBarView_headerCenterRightDrawableVisibility:
                    String visible3 = array.getString(attr);
                    setVisibility(iv_right,visible3);
                    break;
            }
        }
        array.recycle();
        layout_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mClick != null)
                    mClick.leftClick(view);
            }
        });
        layout_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mClick != null)
                    mClick.rightClick(view);
            }
        });
    }

然后我们需要添加左侧,右侧的点击事件,由于一般反馈点击范围太小,导致点击灵敏性较差,所以这里全部设置对左侧,右侧布局进行监听,而不是单个控件

设置回调接口

private onViewClick mClick;

 public void setOnViewClick(onViewClick click) {
        this.mClick = click;
    }

    public interface onViewClick {
        void leftClick();

        void rightClick();
    }

在调用的时候拿到自定义view对象setOnViewClick即可

全部代码如下:

package com.zuji.entrance.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zuji.library.R;

/**
 * Created by fangyc on 2018/7/19.
 */

public class HeaderBarView extends ConstraintLayout {
    private ConstraintLayout layout_left, layout_right;
    private TextView tv_left, tv_title, tv_title2, tv_right;
    private ImageView iv_left, iv_right;
    private onViewClick mClick;

    public HeaderBarView(Context context) {
        this(context, null);
    }

    public HeaderBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderBarView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.zuji_header_bar_view, this);
        initView();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.HeaderBarView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.HeaderBarView_headerLeftTextColor:
                    tv_left.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerLeftDrawble:
                    iv_left.setImageResource(array.getResourceId(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerLeftTextSize:
//                    tv_left.setTextSize(array.getDimension(attr,12f));
                    tv_left.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerLeftText:
                    tv_left.setText(array.getString(attr));
                    break;

                case R.styleable.HeaderBarView_headerCenterTextColor:
                    tv_title.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle:
                    tv_title.setText(array.getString(attr));
                    break;
                case R.styleable.HeaderBarView_headerCenterTextSize:
                    tv_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;

                case R.styleable.HeaderBarView_headerCenterText2Color:
                    tv_title2.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle2:
                    tv_title2.setText(array.getString(attr));
                    break;
                case R.styleable.HeaderBarView_headerCenterText2Size:
//                    tv_title2.setTextSize(array.getDimensionPixelSize(attr, 12));
//                    int size = array.getDimensionPixelSize(attr, 0);
                    tv_title2.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle2Visibility:
                    String visible = array.getString(attr);
                    setVisibility(tv_title2,visible);
                    break;
                case R.styleable.HeaderBarView_headerCenterTitle2DrawableLeft:
                    Drawable drawableLeft = null;
                    drawableLeft = array.getDrawable(attr);
                    /// 这一步必须要做,否则不会显示.
                    drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());
                    tv_title2.setCompoundDrawables(drawableLeft, null, null, null);
                    break;

                case R.styleable.HeaderBarView_headerRightDrawable:
                    iv_right.setImageResource(array.getResourceId(attr, 0));
                    break;
                case R.styleable.HeaderBarView_headerRightText:
                    tv_right.setText(array.getString(attr));
                    break;
                case R.styleable.HeaderBarView_headerRightTextColor:
                    tv_right.setTextColor(array.getColor(attr, Color.BLACK));
                    break;
                case R.styleable.HeaderBarView_headerRightTextSize:
                    tv_right.setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(attr, 0));
                    break;

                case R.styleable.HeaderBarView_headerCenterLeftDrawbleVisibility:
                    String visible2 = array.getString(attr);
                    setVisibility(iv_left,visible2);
                    break;
                case R.styleable.HeaderBarView_headerCenterRightDrawableVisibility:
                    String visible3 = array.getString(attr);
                    setVisibility(iv_right,visible3);
                    break;
            }
        }
        array.recycle();
        layout_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mClick != null)
                    mClick.leftClick(view);
            }
        });
        layout_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mClick != null)
                    mClick.rightClick(view);
            }
        });
    }


    private void setVisibility(View view,String visible){
        visible = TextUtils.isEmpty(visible) ? "visible" : visible;
        switch (visible) {
            case "gone":
                view.setVisibility(View.GONE);
                break;
            case "visible":
                view.setVisibility(View.VISIBLE);
                break;
            case "invisible":
                view.setVisibility(View.INVISIBLE);
                break;
            default:
                view.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void initView() {
        tv_left = findViewById(R.id.tv_left);
        tv_title = findViewById(R.id.tv_title);
        tv_title2 = findViewById(R.id.tv_title_2);
        tv_right = findViewById(R.id.tv_right);
        iv_left = findViewById(R.id.iv_left);
        iv_right = findViewById(R.id.iv_right);
        layout_left = findViewById(R.id.layout_left);
        layout_right = findViewById(R.id.layout_right);
    }

    public void setOnViewClick(onViewClick click) {
        this.mClick = click;
    }

    //设置标题
    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
    }

    //设置左标题
    public void setLeftText(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_left.setText(title);
        }
    }

    //设置右标题
    public void setRightText(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_right.setText(title);
        }
    }

    //设置标题大小
    public void setTitleSize(int size) {
        if (tv_title != null) {
            tv_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    //设置左标题大小
    public void setLeftTextSize(int size) {
        if (tv_left != null) {
            tv_left.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    //设置右标题大小
    public void setRightTextSize(int size) {
        if (tv_right != null) {
            tv_right.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    //设置左图标
    public void setLeftDrawable(int res) {
        if (iv_left != null) {
            iv_left.setImageResource(res);
        }
    }

    //设置右图标
    public void setRightDrawable(int res) {
        if (iv_right != null) {
            iv_right.setImageResource(res);
        }
    }

    public interface onViewClick {
        void leftClick(View view);

        void rightClick(View view);
    }
}

引用如下:

 <com.zuji.entrance.widget.HeaderBarView
        android:id="@+id/title_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/zuji_title_bar_height"
        android:background="#4588f5"
        app:headerCenterTextColor="#FFF"
        app:headerCenterTextSize="14dp"
        app:headerCenterTitle="旅行足迹"

        app:headerCenterTitle2Visibility="gone"
        app:headerLeftDrawble="@drawable/zuji_title_bar_back"
        app:headerRightDrawable="@drawable/zuji_title_bar_more"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
<com.zuji.entrance.widget.HeaderBarView
            android:id="@+id/title_bar"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:background="#ffffff"
            app:headerCenterText2Color="#999999"
            app:headerCenterText2Size="10dp"

            app:headerCenterTextColor="#222222"
            app:headerCenterTextSize="16dp"
            app:headerCenterTitle="广州市"

            app:headerCenterTitle2=" 仅自己可见"
            app:headerCenterTitle2DrawableLeft="@drawable/zuji_privacy_icon"

            app:headerLeftDrawble="@drawable/zuji_header_black_back_default"
            app:headerRightDrawable="@drawable/zuji_black_share_icon_default" />
<com.zuji.entrance.widget.HeaderBarView
        android:id="@+id/title_bar"
        android:layout_width="match_parent"
        android:layout_height="62dp"

        android:background="#ffffff"
        app:headerCenterTextColor="#222222"
        app:headerCenterTextSize="16dp"

        app:headerCenterTitle="点评"
        app:headerCenterTitle2Visibility="gone"
        app:headerLeftText="取消"
        app:headerCenterLeftDrawbleVisibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容