Android顶部标题栏--HeaderBarView

作者: 颤抖的闪电 | 来源:发表于2018-07-20 11:57 被阅读128次
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" />

相关文章

网友评论

    本文标题:Android顶部标题栏--HeaderBarView

    本文链接:https://www.haomeiwen.com/subject/rlvfmftx.html