Android關于BottomNavigationView使用指南
前言
好久不見,計蒙回來了,最近有粉絲投稿了幾個關于BottomNavigationView的一些問題,今天發(fā)篇比較詳細的文章總結(jié)一下,希望能夠?qū)δ阌兴鶐椭?/p>
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、初識BottomNavigationView
常用屬性:
app:itemTextColor 文字的顏色,可以通過selector來控制選中和未選中的顏色
app:itemIconTint 圖標的顏色,可以通過selector來控制選中和未選中的顏色
app:itemIconSize 圖標大小,默認24dp
app:iteamBackground 背景顏色,默認是主題的顏色
app:itemRippleColor 點擊后的水波紋顏色
app:itemTextAppearanceActive 設置選中時文字樣式
app:itemTextAppearanceInactive 設置默認的文字樣式
app:itemHorizontalTranslationEnabled 在label visibility 模式為
selected
時item水平方向移動app:elevation 控制控件頂部的陰影
app:labelVisibilityMode 文字的顯示模式
app:menu 指定菜單xml文件(文字和圖片都寫在這個里面)
在Android Studio創(chuàng)建新項目時,會有很多小伙伴在模塊中選擇此類型的Activity,如下。
項目運行效果圖如下:
二、BottomNavigationView中的顏色關鍵實現(xià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); }
可以很明顯的看到起到關鍵作用的是ColorStateList,而處理好這個傳入的參數(shù)即可解決顏色問題。
三、開始解決問題
1.如何修改圖標顏色
這里提供兩種解決方式
xml中解決:
首先:新建一個selector_color文件,設置兩種狀態(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文件中解決:
傳入一個自定義的ColorStateList。
并將其以參數(shù)傳入view中
navView.setItemIconTintList();
2.如何使圖標點擊顏色不改變
在java中調(diào)用其setItemIconTintList,傳參為空即可
navView.setItemIconTintList(null);
3.如何使點擊時字體不改變大小
在dimens文件中設置以下兩個的值為同一大小即可
//防止字體出現(xiàn)變大效果 <dimen name="design_bottom_navigation_active_text_size">10dp</dimen> <dimen name="design_bottom_navigation_text_size">10dp</dimen>
4.當你的圖標是多色系時
在java中調(diào)用其setItemIconTintList,傳參為空
navView.setItemIconTintList(null);
然后設置圖片狀態(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é)省時間,只修改了第一個,效果如下
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.設置APP樣式為NoActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
四、總結(jié)
到此這篇關于Android關于BottomNavigationView使用指南的文章就介紹到這了,更多相關Android BottomNavigationView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法
Intent能夠?qū)崿F(xiàn)應用間的數(shù)據(jù)交互與通訊,將實現(xiàn)者和調(diào)用者解耦,接下來就來詳解Android中通過Intent類實現(xiàn)組件間調(diào)用的方法,需要的朋友可以參考下2016-05-05Android APK使用Debug簽名重新打包 Eclipse更改默認Debug簽名
這篇文章主要介紹了Android APK使用Debug簽名重新打包 Eclipse更改默認Debug簽名等內(nèi)容,需要的朋友可以參考下2015-04-04FlowLayout流式布局實現(xiàn)搜索清空歷史記錄
這篇文章主要為大家詳細介紹了FlowLayout流式布局實現(xiàn)搜索清空歷史記錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android自定義View實現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05Android TableLayout數(shù)據(jù)列表的回顯清空實現(xiàn)思路及代碼
數(shù)據(jù)列表的回顯必須從后面減去子元素同時必須從后面減去子元素,感興趣的朋友可以看下具體的實現(xiàn)代碼,希望對你學習Android TableLayout有所幫助2013-04-04Android 出現(xiàn)的警告(Service Intent must be explicit)解決辦法詳解
這篇文章主要介紹了Android 出現(xiàn)的警告(Service Intent must be explicit)解決辦法詳解的相關資料,需要的朋友可以參考下2017-04-04