Android關(guān)于BottomNavigationView使用指南
前言
好久不見,計(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,如下。
項(xiàng)目運(yùn)行效果圖如下:
二、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è),效果如下
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)文章
詳解Android中通過Intent類實(shí)現(xiàn)組件間調(diào)用的方法
Intent能夠?qū)崿F(xiàn)應(yīng)用間的數(shù)據(jù)交互與通訊,將實(shí)現(xiàn)者和調(diào)用者解耦,接下來就來詳解Android中通過Intent類實(shí)現(xiàn)組件間調(diào)用的方法,需要的朋友可以參考下2016-05-05Android APK使用Debug簽名重新打包 Eclipse更改默認(rèn)Debug簽名
這篇文章主要介紹了Android APK使用Debug簽名重新打包 Eclipse更改默認(rèn)Debug簽名等內(nèi)容,需要的朋友可以參考下2015-04-04FlowLayout流式布局實(shí)現(xiàn)搜索清空歷史記錄
這篇文章主要為大家詳細(xì)介紹了FlowLayout流式布局實(shí)現(xiàn)搜索清空歷史記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android UI控件之ProgressBar進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android UI控件之ProgressBar進(jìn)度條的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android AlertDialog對話框詳解及實(shí)例
這篇文章主要介紹了Android AlertDialog對話框詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12Android TableLayout數(shù)據(jù)列表的回顯清空實(shí)現(xiàn)思路及代碼
數(shù)據(jù)列表的回顯必須從后面減去子元素同時(shí)必須從后面減去子元素,感興趣的朋友可以看下具體的實(shí)現(xiàn)代碼,希望對你學(xué)習(xí)Android TableLayout有所幫助2013-04-04Android 出現(xiàn)的警告(Service Intent must be explicit)解決辦法詳解
這篇文章主要介紹了Android 出現(xiàn)的警告(Service Intent must be explicit)解決辦法詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04