android實(shí)現(xiàn)簡(jiǎn)單底部導(dǎo)航欄
本文實(shí)例為大家分享了android實(shí)現(xiàn)底部導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
常見的底部導(dǎo)航欄

動(dòng)態(tài)效果

實(shí)現(xiàn)步驟
1.底部導(dǎo)航欄樣式
我們應(yīng)該在項(xiàng)目的res文件夾下新建一個(gè)menu文件夾,用來裝menu布局文件
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <item ? ? ? ? android:id="@+id/navigation_home" ? ? ? ? android:icon="@drawable/international_1" ? ? ? ? android:title="主頁" /> ? ? <item ? ? ? ? android:id="@+id/navigation_edit" ? ? ? ? android:icon="@drawable/edit_0" ? ? ? ? android:title="發(fā)布" /> ? ? <item ? ? ? ? android:id="@+id/navigation_view" ? ? ? ? android:icon="@drawable/view_0" ? ? ? ? android:title="關(guān)注" /> ? ? <item ? ? ? ? android:id="@+id/navigation_user" ? ? ? ? android:icon="@drawable/user_0" ? ? ? ? android:title="我的" /> </menu>

2.新建四個(gè)fragment組件
每一個(gè)fragment的組件內(nèi)容相同

四個(gè)fragement對(duì)應(yīng)的layout
四個(gè)fragment布局文件的內(nèi)容也相同,寫上內(nèi)容以區(qū)別是哪個(gè)頁面
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <TextView ? ? ? ? android:id="@+id/text_home" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginStart="8dp" ? ? ? ? android:layout_marginTop="8dp" ? ? ? ? android:layout_marginEnd="8dp" ? ? ? ? android:textAlignment="center" ? ? ? ? android:textSize="20sp" ? ? ? ? app:layout_constraintEnd_toEndOf="parent" ? ? ? ? app:layout_constraintStart_toStartOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent" ? ? ? ? android:text="this is homebar" ? ? ? ? /> </androidx.constraintlayout.widget.ConstraintLayout>

3.建議navigation布局文件(至關(guān)重要)
這個(gè)文件指定了頁面上顯式那些fragment組件
在項(xiàng)目res下新建一個(gè)文件夾專門用來存放此文件

id取值一定要與底部導(dǎo)航欄樣式里面指定的ID相同,因?yàn)閍ndroid自動(dòng)根據(jù)底部按鈕的ID來綁定按鈕與fragment
<?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/mobile_navigation" ? ? app:startDestination="@+id/navigation_home"> ? ? <fragment ? ? android:id="@+id/navigation_home" ? ? android:name="cn.liuhao.test.fragments.HomeFragment" ? ? tools:layout="@layout/fragment_home" /> ? ? <fragment ? ? ? ? android:id="@+id/navigation_view" ? ? ? ? android:name="cn.liuhao.test.fragments.ViewFragment" ? ? ? ? tools:layout="@layout/fragment_view" /> ? ? <fragment ? ? ? ? android:id="@+id/navigation_edit" ? ? ? ? android:name="cn.liuhao.test.fragments.EditFragment" ? ? ? ? tools:layout="@layout/fragment_eidt" /> ? ? <fragment ? ? ? ? android:id="@+id/navigation_user" ? ? ? ? android:name="cn.liuhao.test.fragments.UserFragment" ? ? ? ? tools:layout="@layout/fragment_user" /> </navigation>
4.activity
布局文件
<?xml version="1.0" encoding="utf-8"?> <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"> ?? ? ?? ?<!-- 底部導(dǎo)航欄 --> ? ? <com.google.android.material.bottomnavigation.BottomNavigationView ? ? ? ? android:id="@+id/nav_view" ? ? ? ? android:layout_width="0dp" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginStart="0dp" ? ? ? ? android:layout_marginEnd="0dp" ? ? ? ? android:background="?android:attr/windowBackground" ? ? ? ? app:layout_constraintBottom_toBottomOf="parent" ? ? ? ? app:layout_constraintLeft_toLeftOf="parent" ? ? ? ? app:layout_constraintRight_toRightOf="parent" ? ? ? ? app:menu="@menu/bottom_nav_menu" /> ?? ? ?? ?<!-- 頁面中顯式fragment的容器--> ? ? <fragment ? ? ? ? android:id="@+id/nav_host_fragment" ? ? ? ? android:name="androidx.navigation.fragment.NavHostFragment" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? app:defaultNavHost="true" ? ? ? ? app:layout_constraintBottom_toTopOf="@id/nav_view" ? ? ? ? app:layout_constraintLeft_toLeftOf="parent" ? ? ? ? app:layout_constraintRight_toRightOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent" ? ? ? ? app:navGraph="@navigation/mobile_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout>
內(nèi)容(綁定Navigation與BottomNavigationView)
public class Main2Activity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main2);
? ? ? ? // 獲取頁面上的底部導(dǎo)航欄控件
? ? ? ? BottomNavigationView navView = findViewById(R.id.nav_view);
? ? ? ? // 配置navigation與底部菜單之間的聯(lián)系
? ? ? ? // 底部菜單的樣式里面的item里面的ID與navigation布局里面指定的ID必須相同,否則會(huì)出現(xiàn)綁定失敗的情況
? ? ? ? AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
? ? ? ? ? ? ? ? R.id.navigation_home,R.id.navigation_edit,R.id.navigation_view,R.id.navigation_user)
? ? ? ? ? ? ? ? .build();
? ? ? ? // 建立fragment容器的控制器,這個(gè)容器就是頁面的上的fragment容器
? ? ? ? NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
? ? ? ??
? ? ? ? // 啟動(dòng)
? ? ? ? NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
? ? ? ? NavigationUI.setupWithNavController(navView, navController);
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法分析
這篇文章主要介紹了Android開發(fā)之圖片壓縮實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android圖片壓縮的原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03
Android6.0 storage目錄sd卡存儲(chǔ)的路徑創(chuàng)建詳解
這篇文章主要介紹了Android6.0 storage目錄sd卡存儲(chǔ)的路徑創(chuàng)建的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android數(shù)據(jù)加密之異或加密算法的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狝ndroid數(shù)據(jù)加密之異或加密算法的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Android實(shí)現(xiàn)帶有邊框的ListView和item的方法
這篇文章主要介紹了Android實(shí)現(xiàn)帶有邊框的ListView和item的方法,結(jié)合實(shí)例形式分析了ListView和item四周添加邊框的實(shí)現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07
flutter 實(shí)現(xiàn)多布局列表的示例代碼
這篇文章主要介紹了flutter 實(shí)現(xiàn)多布局列表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

