Android底部導(dǎo)航組件BottomNavigationView
什么是BottomNavigationView
底部菜單欄

BottomNavigationView的簡(jiǎn)單用法
需求:如上圖所示。點(diǎn)擊測(cè)試一菜單,展示test1fragment。點(diǎn)擊測(cè)試二菜單,展示test2fragment。點(diǎn)擊測(cè)試三菜單,展示test3fragment。
第一步,testActivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
// 容器,承載fragment
<FrameLayout
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
// BottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu_test" />
</LinearLayout>
第二步,寫B(tài)ottomNavigationView所需要的菜單
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_test1"
android:icon="@drawable/ic_home_black_24dp"
android:title="測(cè)試一" />
<item
android:id="@+id/navigation_test2"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="測(cè)試二" />
<item
android:id="@+id/navigation_test3"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="測(cè)試三" />
</menu>
第三步,書寫testActivity文件。重點(diǎn)是setOnNavigationItemSelectedListener點(diǎn)擊事件
public class TestActivity extends AppCompatActivity {
List<Fragment> mFragments = new ArrayList<>();
test1Fragment t1f = new test1Fragment();
test2Fragment t2f = new test2Fragment();
test3Fragment t3f = new test3Fragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
BottomNavigationView navView = findViewById(R.id.nav_view);
mFragments.add(t1f);
mFragments.add(t2f);
mFragments.add(t3f);
// navView 點(diǎn)擊事件
navView.setOnNavigationItemSelectedListener((item)->{
switchFragment(item.getItemId());
return true;
});
}
private void switchFragment(int id) {
Fragment fragment = null;
switch (id) {
case R.id.navigation_test1:
fragment = mFragments.get(0);
break;
case R.id.navigation_test2:
fragment = mFragments.get(1);
break;
case R.id.navigation_test3:
fragment = mFragments.get(2);
break;
default:
break;
}
if (fragment != null) {
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,fragment).commit();
}
}
}到此這篇關(guān)于Android底部導(dǎo)航組件BottomNavigationView的文章就介紹到這了,更多相關(guān)Android BottomNavigationView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DataBinding onClick的七種點(diǎn)擊方式
這篇文章主要給大家介紹了關(guān)于DataBinding onClick的七種點(diǎn)擊方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法
這篇文章主要介紹了Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-07-07
Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 應(yīng)用的全屏和非全屏實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android中使用GridView進(jìn)行應(yīng)用程序UI布局的教程
GridView即平常我們見到的類似九宮格的矩陣型布局,只不過默認(rèn)不帶分割線,這里我們就從基礎(chǔ)開始來看一下Android中使用GridView進(jìn)行應(yīng)用程序UI布局的教程2016-06-06
Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框(二)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
android中圖片翻頁效果簡(jiǎn)單的實(shí)現(xiàn)方法
android中圖片翻頁效果簡(jiǎn)單的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-05-05
Android修行手冊(cè)之ConstraintLayout布局使用詳解
這篇文章主要為大家介紹了Android修行手冊(cè)之ConstraintLayout使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android ListView 條目多樣式展示實(shí)例詳解
這篇文章主要介紹了Android ListView 條目多樣式展示的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android?Studio中如何修改APP圖標(biāo)和APP名稱
這篇文章主要介紹了Android?Studio中如何修改APP圖標(biāo)和APP名稱,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

