欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android關(guān)于BottomNavigationView使用指南

 更新時(shí)間:2022年01月12日 10:59:46   作者:計(jì)蒙不吃魚  
本文主要介紹了Android關(guān)于BottomNavigationView使用指南,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

好久不見,計(jì)蒙回來了,最近有粉絲投稿了幾個(gè)關(guān)于BottomNavigationView的一些問題,今天發(fā)篇比較詳細(xì)的文章總結(jié)一下,希望能夠?qū)δ阌兴鶐椭?/p>

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、初識BottomNavigationView

常用屬性:

  • app:itemTextColor 文字的顏色,可以通過selector來控制選中和未選中的顏色

  • app:itemIconTint 圖標(biāo)的顏色,可以通過selector來控制選中和未選中的顏色

  • app:itemIconSize 圖標(biāo)大小,默認(rèn)24dp

  • app:iteamBackground 背景顏色,默認(rèn)是主題的顏色

  • app:itemRippleColor 點(diǎn)擊后的水波紋顏色

  • app:itemTextAppearanceActive 設(shè)置選中時(shí)文字樣式

  • app:itemTextAppearanceInactive 設(shè)置默認(rèn)的文字樣式

  • app:itemHorizontalTranslationEnabled 在label visibility 模式為selected時(shí)item水平方向移動

  • app:elevation 控制控件頂部的陰影

  • app:labelVisibilityMode 文字的顯示模式

  • app:menu 指定菜單xml文件(文字和圖片都寫在這個(gè)里面)

在Android Studio創(chuàng)建新項(xiàng)目時(shí),會有很多小伙伴在模塊中選擇此類型的Activity,如下。

請?zhí)砑訄D片描述

項(xiàng)目運(yùn)行效果圖如下:

請?zhí)砑訄D片描述

二、BottomNavigationView中的顏色關(guān)鍵實(shí)現(xiàn)代碼解析(舉例)

是如何定義的顏色的。

關(guān)鍵代碼如下(獲取xml中的屬性):

 ColorStateList backgroundTint =
        MaterialResources.getColorStateList(
            context, a, R.styleable.BottomNavigationView_backgroundTint);
    DrawableCompat.setTintList(getBackground().mutate(), backgroundTint);

    setLabelVisibilityMode(
        a.getInteger(
            R.styleable.BottomNavigationView_labelVisibilityMode,
            LabelVisibilityMode.LABEL_VISIBILITY_AUTO));
    setItemHorizontalTranslationEnabled(
        a.getBoolean(R.styleable.BottomNavigationView_itemHorizontalTranslationEnabled, true));

    int itemBackground = a.getResourceId(R.styleable.BottomNavigationView_itemBackground, 0);
    if (itemBackground != 0) {
      menuView.setItemBackgroundRes(itemBackground);
    } else {
      ColorStateList itemRippleColor =
          MaterialResources.getColorStateList(
              context, a, R.styleable.BottomNavigationView_itemRippleColor);
      setItemRippleColor(itemRippleColor);
    }

可以很明顯的看到起到關(guān)鍵作用的是ColorStateList,而處理好這個(gè)傳入的參數(shù)即可解決顏色問題。

三、開始解決問題

1.如何修改圖標(biāo)顏色

這里提供兩種解決方式
xml中解決
首先:新建一個(gè)selector_color文件,設(shè)置兩種狀態(tài)的顏色

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#63F7DE" android:state_checked="true"  />
    <item android:color="@android:color/black" android:state_checked="false"/>
</selector>

然后在BottomNavigationView中調(diào)用此文件

app:itemIconTint="@color/selector_color"

java文件中解決:
傳入一個(gè)自定義的ColorStateList。
并將其以參數(shù)傳入view中

navView.setItemIconTintList();

2.如何使圖標(biāo)點(diǎn)擊顏色不改變

在java中調(diào)用其setItemIconTintList,傳參為空即可

navView.setItemIconTintList(null);

3.如何使點(diǎn)擊時(shí)字體不改變大小

在dimens文件中設(shè)置以下兩個(gè)的值為同一大小即可

    //防止字體出現(xiàn)變大效果
    <dimen name="design_bottom_navigation_active_text_size">10dp</dimen>
    <dimen name="design_bottom_navigation_text_size">10dp</dimen>

4.當(dāng)你的圖標(biāo)是多色系時(shí)

在java中調(diào)用其setItemIconTintList,傳參為空

navView.setItemIconTintList(null);

然后設(shè)置圖片狀態(tài)的item中drawable的選擇,舉例如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_home_black_24dp" android:state_checked="true"  />
    <item android:drawable="@drawable/ic_home_black_false_24dp" android:state_checked="false"/>
</selector>

最后在menu中調(diào)用此文件即可。舉例文件名為:ic_home

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

為了節(jié)省時(shí)間,只修改了第一個(gè),效果如下

請?zhí)砑訄D片描述

5.不想要ActionBar

1.將xml中paddingTop這行刪除

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

2.在java中將以下這行刪除

NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

3.設(shè)置APP樣式為NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

四、總結(jié)

到此這篇關(guān)于Android關(guān)于BottomNavigationView使用指南的文章就介紹到這了,更多相關(guān)Android BottomNavigationView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論