赞
踩
Activity中最常见的页面结构就是顶部返回键+标题(+副标题)+右侧的菜单/编辑按钮/“提交”/“发布”,将此结构封装并设置多个属性,使用时一个View即可搞定。
图一实现代码
<包名.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" />
<!-- 顶部导航栏-->
<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>
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; } }
<?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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。