Android Design 25.0.0 BottomNavigationView

前言

翻看com.android.support:design:25.0.0看到提供了几个组件封装库,觉得BottomNavigationView能够引入到项目中,代码也不复杂,就拿出来进行一个快速介绍,方便后面用到该组件的伙伴,有个大致了解

效果1

这里写图片描述

效果2

这里写图片描述

效果三

这里写图片描述

引入方式

1.依赖包

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:design:25.0.0'
}

2.引入布局中

<?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:design="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ppisland.servenshare.MainActivity">



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        design:itemIconTint="@color/item_text_color"
        design:itemTextColor="@color/item_text_color"
        design:menu="@menu/menu_navigation" />

</android.support.constraint.ConstraintLayout>

注意下该组件自定义属性

        design:itemIconTint="@color/item_text_color"
        design:itemTextColor="@color/item_text_color"
        design:menu="@menu/menu_navigation" 


        app:itemBackground : 子菜单背景 
        app:itemIconTint : 图标颜色 
        app:itemTextColor : 文本颜色 
        app:menu : 菜单

3.系统扩展文件 values.xml

<declare-styleable name="BottomNavigationView">
<attr name="menu"/>
<attr name="itemIconTint"/>
<attr name="itemTextColor"/>
<attr name="itemBackground"/>

</declare-styleable>    

4.配置选中态颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#fff" android:state_checked="true"/>
    <item android:color="#fff" android:state_pressed="true"/>
    <item android:color="#ccc"/>
</selector>

5.图标使用的矢量图形式,当然可以设置drable样式icon

<

vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 
        0.67,-0.36 1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55
        -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1
        0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
</vector>

6.定义item条目,这里使用menu方式进行定义,并非私用布局容器定义(常用的布局容器)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item1"
        android:checked="true"
        android:icon="@drawable/ic_mail_outline_black_24dp"
        android:title="Message"/>

    <item
        android:id="@+id/item2"
        android:icon="@drawable/ic_call_black_24dp"
        android:title="Call"/>

    <item
        android:id="@+id/item3"
        android:icon="@drawable/ic_person_black_24dp"
        android:title="Contact"/>

</menu>

ok ,定义三个item,看效果如上面效果图

事件监听一并提供。类似onclick,不做介绍。

setOnNavigationItemSelectedListener

放入三个item没问题那多放入几个,(官方建议3~5个子item)

五个效果如下

效果2

这里写图片描述

你妹的,这样可不好,效果太玄,不符合我们app风格。怎么办呢改。

网上现成代码,利用反射修改,动画模式。(下面是kotlin代码,用法一样)

open class BottomNavigationViewHelper {

    @SuppressLint("RestrictedApi")
    fun disableShiftMode(view: BottomNavigationView) {
        val menuView = view.getChildAt(0) as BottomNavigationMenuView
        try {
            val shiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
            shiftingMode.isAccessible = true
            shiftingMode.setBoolean(menuView, false)
            shiftingMode.isAccessible = false
            for (i in 0 until menuView.childCount) {
                val item = menuView.getChildAt(i) as BottomNavigationItemView

                item.setShiftingMode(false)
                // set once again checked value, so view will be updated

                item.setChecked(item.itemData.isChecked)
            }
        } catch (e: NoSuchFieldException) {
            Log.e("BNVHelper", "Unable to get shift mode field", e)
        } catch (e: IllegalAccessException) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e)
        }

    }
}

调用方式,将BottomNavigationView引用放入就关闭动画效果了

bottomHelp!!.disableShiftMode(navigation)

仔细的同学,会发现,切换时候字体也是随着selected,unselected 进行缩放的,这个好办!!!

看下源码

 public BottomNavigationItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final Resources res = getResources();
        int inactiveLabelSize =
                res.getDimensionPixelSize(R.dimen.design_bottom_navigation_text_size);
        int activeLabelSize = res.getDimensionPixelSize(
                R.dimen.design_bottom_navigation_active_text_size);
        mDefaultMargin = res.getDimensionPixelSize(R.dimen.design_bottom_navigation_margin);
        mShiftAmount = inactiveLabelSize - activeLabelSize;
        mScaleUpFactor = 1f * activeLabelSize / inactiveLabelSize;
        mScaleDownFactor = 1f * inactiveLabelSize / activeLabelSize;

        LayoutInflater.from(context).inflate(R.layout.design_bottom_navigation_item, this, true);
        setBackgroundResource(R.drawable.design_bottom_navigation_item_background);
        mIcon = (ImageView) findViewById(R.id.icon);
        mSmallLabel = (TextView) findViewById(R.id.smallLabel);
        mLargeLabel = (TextView) findViewById(R.id.largeLabel);

    }

看到什么了?

这里我们对系统定义的dimens进行同名覆盖,在项目dimens.xml 中新建同名属性

 <?xml version="1.0" encoding="utf-8"?>

    <resources>
        <dimen name="design_bottom_navigation_text_size">12sp</dimen>
        <dimen name="design_bottom_navigation_active_text_size">12sp</dimen>
    </resources>

扩展

看到有个同学在原基础上进行了BadgeView ,红点扩展,采用了位置测量方案。这个实现方式多样。不做介绍

效果三样式

引用

官方地址 https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html

https://www.pgyer.com/bottomview 蒲公英地址

http://blog.csdn.net/qq_35064774/article/details/54177702 一种实现方式

猜你喜欢

转载自blog.csdn.net/o279642707/article/details/79005950