当前位置:   article > 正文

Android自定义View 顶部导航栏_android navbar

android navbar

Activity中最常见的页面结构就是顶部返回键+标题(+副标题)+右侧的菜单/编辑按钮/“提交”/“发布”,将此结构封装并设置多个属性,使用时一个View即可搞定。

实现效果

实现效果1
实现效果2
图一实现代码

<包名.widgets.NavBar
        android:id="@+id/nav_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:leftIconColor="@color/black"
        app:navBackground="@color/background"
        app:navTitle="详情"
        app:navTitleColor="@color/black"
        app:rightIcon="@drawable/collect"
        app:showBack="true" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
attrs.xml
    <!--    顶部导航栏-->
    <declare-styleable name="NavBar">
        <attr name="showBack" format="boolean" /><!--是否展示背景-->
        <attr name="navTitle" format="string" /><!--标题文本-->
        <attr name="navTitleColor" format="color" /><!--标题颜色-->
        <attr name="navSubTitle" format="string" /><!--副标题文本-->
        <attr name="leftIcon" format="reference" /><!--左侧图标-->
        <attr name="leftIconColor" format="color" /><!--左侧图标颜色-->
        <attr name="showLeftText" format="boolean" /><!--左侧文本-->
        <attr name="rightIcon" format="reference" /><!--右侧图标-->
        <attr name="navBackground" format="color" /><!--背景色-->
        <attr name="navRightText" format="string" /><!--右侧文本-->
        <attr name="navRightTextColor" format="color" /><!--右侧文本颜色-->
    </declare-styleable>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
NavBar.java
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

/*添加import R类*/

/**
 * 顶部导航栏
 */
public class NavBar extends FrameLayout {

    private ImageView mIvLeft, mIvRight;
    private TextView mTvTitle, mTvSubTitle, mTvRight, mTvLeft;
    private RelativeLayout mRlRoot;

    /**
     * 显示返回
     */
    private boolean mShowBack;
    /**
     * 显示取消
     */
    private boolean mShowLeftText;
    @DrawableRes
    private int mLeftSrc, mRightSrc;
    @ColorInt
    private int mBackgroundColor, mTitleColor, mLeftColor, mRightTextColor;
    private String mTitle, mSubTitle;


    public NavBar(@NonNull Context context) {
        this(context, null, 0);
    }

    public NavBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavBar(@NonNull final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // init
        //高度WrapContent
        final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
                new int[]{android.R.attr.actionBarSize});
        styledAttributes.recycle();

        String textRight = null;
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.NavBar, defStyleAttr, 0);
            mShowBack = typedArray.getBoolean(R.styleable.NavBar_showBack, false);
            mTitle = typedArray.getString(R.styleable.NavBar_navTitle);
            mTitleColor = typedArray.getColor(R.styleable.NavBar_navTitleColor, 0);
            mLeftColor = typedArray.getColor(R.styleable.NavBar_leftIconColor, 0);
            mShowLeftText = typedArray.getBoolean(R.styleable.NavBar_showLeftText, false);
            mRightTextColor = typedArray.getColor(R.styleable.NavBar_navRightTextColor, 0);
            mSubTitle = typedArray.getString(R.styleable.NavBar_navSubTitle);
            mLeftSrc = typedArray.getResourceId(R.styleable.NavBar_leftIcon, 0);
            mRightSrc = typedArray.getResourceId(R.styleable.NavBar_rightIcon, 0);
            mBackgroundColor = typedArray.getColor(R.styleable.NavBar_navBackground,
                    ContextCompat.getColor(context, R.color.colorPrimary));
            textRight = typedArray.getString(R.styleable.NavBar_navRightText);
            typedArray.recycle();
        }


        View v = LayoutInflater.from(context).inflate(R.layout.base_view_top_bar, this, false);
        mRlRoot = v.findViewById(R.id.rl_root);
        mTvTitle = v.findViewById(R.id.base_tv_title);
        mTvSubTitle = v.findViewById(R.id.base_tv_sub_title);
        mIvLeft = v.findViewById(R.id.iv_left);
        mTvLeft = v.findViewById(R.id.tv_left);
        mIvRight = v.findViewById(R.id.iv_right);
        mTvRight = v.findViewById(R.id.tv_right);

        if (mTitleColor != 0) {
            mTvTitle.setTextColor(mTitleColor);
        }
        if (mLeftColor != 0) {
            mIvLeft.setColorFilter(mLeftColor);
        }

        v.setBackgroundColor(mBackgroundColor);

        if (mShowBack) {
            mIvLeft.setVisibility(VISIBLE);
            mIvLeft.setImageResource(R.drawable.base_ic_back_white);
            mIvLeft.setOnClickListener((view) -> {
                ((Activity) context).onBackPressed();
            });
            if (mShowLeftText) {
                mTvLeft.setVisibility(VISIBLE);
                mTvLeft.setOnClickListener((view) -> {
                    if (context instanceof Activity) {
                        ((Activity) context).onBackPressed();
                    }
                });
            }
        } else {
            if (mLeftSrc != 0) {
                mIvLeft.setVisibility(VISIBLE);
                mIvLeft.setImageResource(mLeftSrc);
            } else {
                mIvLeft.setVisibility(INVISIBLE);
            }
        }

        if (mSubTitle != null) {
            mTvSubTitle.setText(mSubTitle);
            mTvSubTitle.setVisibility(VISIBLE);
        } else {
            mTvSubTitle.setVisibility(GONE);
        }

        if (mRightSrc != 0) {
            mIvRight.setVisibility(VISIBLE);
            mIvRight.setImageResource(mRightSrc);
        } else {
            mIvRight.setVisibility(INVISIBLE);
        }

        if (textRight != null) {
            mTvRight.setVisibility(VISIBLE);
            mTvRight.setText(textRight);
            if (mRightTextColor != 0) {
                mTvRight.setTextColor(mRightTextColor);
            }
        } else {
            mTvRight.setVisibility(GONE);
        }

        mTvTitle.setText(mTitle);

        addView(v);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            measureChildren(widthMeasureSpec, heightMeasureSpec);

            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
                    MeasureSpec.getSize(heightMeasureSpec));
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            measureChildren(widthMeasureSpec, heightMeasureSpec);
        }

    }

    @Override
    protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        super.measureChildren(widthMeasureSpec, heightMeasureSpec);
    }

    public RelativeLayout getRoot() {
        return mRlRoot;
    }

    /**
     * 获取左边imageView
     */
    public ImageView getLeftImageView() {
        return mIvLeft;
    }

    /**
     * 获取右边ImageView
     */
    public ImageView getRightImageView() {
        return mIvRight;
    }

    /**
     * 获取标题
     *
     * @return {@link TextView}
     */
    public TextView getTitleTextView() {
        return mTvTitle;
    }

    /**
     * 获取副标题
     */
    public TextView getSubTitleTextView() {
        return mTvSubTitle;
    }

    /**
     * 获取左面的TextView
     */
    public TextView getLeftTextView() {
        return mTvLeft;
    }

    /**
     * 获取右侧文本
     */
    public TextView getRightTextView() {
        return mTvRight;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
base_view_top_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_root"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/transparent"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    tools:background="@color/colorPrimary">

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="30dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:scaleType="centerInside"
        android:src="@drawable/base_ic_back_dark" />

    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/iv_left"
        android:text="取消"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/iv_right"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:lines="1"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:singleLine="true"
        android:textColor="#FF333333"
        android:textSize="18sp"
        android:visibility="gone"
        tools:text="确定" />


    <TextView
        android:id="@+id/base_tv_sub_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:drawablePadding="3dp"
        android:lines="1"
        android:singleLine="true"
        android:textColor="#FF333333"
        android:textSize="10sp"
        android:visibility="gone" />


    <TextView
        android:id="@+id/base_tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/base_tv_sub_title"
        android:layout_centerInParent="true"
        android:layout_marginStart="30dp"
        android:layout_marginEnd="30dp"
        android:layout_toLeftOf="@id/iv_right"
        android:layout_toRightOf="@id/iv_left"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="1"
        android:singleLine="true"
        android:textColor="@color/black"
        android:textFontWeight="500"
        android:textSize="18sp" />
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/219263
推荐阅读
相关标签
  

闽ICP备14008679号