Android BottomNavigationView底部導(dǎo)航欄的使用

基礎(chǔ)使用

先導(dǎo)入依賴(這里以AndroidX為例)

implementation 'com.google.android.material:material:1.0.0'

在布局中使用

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="@color/white"
            app:itemBackground="@null"
            app:itemTextColor="@drawable/sl_tab_color"
            app:labelVisibilityMode="labeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:menu="@menu/navigation_item" />

其中app:itemTextColor="@drawable/sl_tab_color"為設(shè)置按鈕選中與未選中時(shí)字體的顏色選擇。navigation_item.xml文件為:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_main"
        android:icon="@drawable/sl_tab_main_main"
        android:title="@string/menu_main"/>

    <item
        android:id="@+id/menu_project"
        android:icon="@drawable/sl_tab_main_project"
        android:title="@string/menu_project"/>

    <item
        android:id="@+id/menu_wechat"
        android:icon="@drawable/sl_tab_main_wechat"
        android:title="@string/menu_wechat"/>

    <item
        android:id="@+id/menu_system"
        android:icon="@drawable/sl_tab_main_system"
        android:title="@string/menu_system"/>
</menu>

其中android:icon="@drawable/sl_tab_main_system"為設(shè)置按鈕選中與未選中時(shí)的圖標(biāo)選擇
顯示效果:


image.png

注意:

在BottomNavigationView的源碼內(nèi)有如下一段代碼

public int getMaxItemCount() {
        return 5;
}

所以當(dāng)我們?cè)趍enu文件內(nèi)添加的item個(gè)數(shù)大于5個(gè)時(shí)會(huì)報(bào)錯(cuò)

常見的效果顯示

1.去除點(diǎn)擊時(shí)出現(xiàn)的水波紋背景效果
app:itemBackground="@null"
2.去除點(diǎn)擊時(shí)的動(dòng)畫效果以及位移現(xiàn)象
app:labelVisibilityMode="labeled"

屬性取值有l(wèi)abeled、unlabeled、selected、auto
labeled:所有按鈕中都顯示圖標(biāo)與標(biāo)簽。
unlabeled:所有按鈕中都不顯示標(biāo)簽,只顯示圖標(biāo)。
selected:只有選中的按鈕才顯示標(biāo)簽與圖標(biāo),其他未選中的只顯示圖標(biāo)。
auto:自動(dòng)模式,該模式使用item數(shù)來確定是否顯示或隱藏標(biāo)簽。當(dāng)按鈕個(gè)數(shù)小于等于3個(gè)時(shí)使用labeled模式,大于3個(gè)時(shí)使用selected模式。

此方法僅限當(dāng)前使用的api大于28時(shí)才可使用。當(dāng)api小于28時(shí)可采用下面這種方式:

@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
    try {
        Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
        shiftingMode.setAccessible(true);
        shiftingMode.setBoolean(menuView, false);
        shiftingMode.setAccessible(false);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
            item.setShiftingMode(false); 
            item.setChecked(item.getItemData().isChecked());
        }
    } catch (NoSuchFieldException e) {
        Log.e( "TAG","Unable to get shift mode field");
    } catch (IllegalAccessException e) {
        Log.e( "TAG","Unable to change value of shift mode");
    }
}
3.去除點(diǎn)擊時(shí)默認(rèn)的圖標(biāo)顏色修改
bottomNavigationView.itemIconTintList = null
4.修改文字大小

在資源文件dimens.xml中添加

<dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
<dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>

Activity內(nèi)使用

bottomNavigationView.setOnNavigationItemSelectedListener {
         when (it.itemId) {
                R.id.menu_main -> {
                         //todo
                }
                R.id.menu_project -> {
                        //todo
                }
                R.id.menu_system -> {
                           //todo
                }
                R.id.menu_wechat -> {
                          //todo
                }
            }
            true
        }

如果需要獲取具體某個(gè)按鈕的相關(guān)信息時(shí),可以調(diào)用下面的代碼


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容