android實(shí)現(xià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); ? ? } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android如何設(shè)置Activity背景色為透明色
本篇文章主要介紹了android如何設(shè)置Activity背景色為透明色,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07詳解Android應(yīng)用開發(fā)--MP3音樂播放器代碼實(shí)現(xiàn)(一)
這篇文章主要介紹了詳解Android應(yīng)用開發(fā)--MP3音樂播放器代碼實(shí)現(xiàn)(一),非常具有實(shí)用價(jià)值,需要的朋友可以參考下 。2017-01-01Android如何調(diào)整線程調(diào)用棧大小
這篇文章主要介紹了Android如何調(diào)整線程調(diào)用棧大小,幫助大家更好的進(jìn)行Android開發(fā),完善自身程序,感興趣的朋友可以了解下2020-10-10Android上傳文件到服務(wù)端并顯示進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android上傳文件到服務(wù)端,并顯示進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)戰(zhàn)教程第九篇之短信高效備份
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第九篇之短信高效備份,利用xml序列化器備份短信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11解決Android SearchView不顯示搜索icon的問題
這篇文章主要介紹了解決Android SearchView不顯示搜索icon問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05淺談Android Studio 4.1 更新內(nèi)容
這篇文章主要介紹了淺談Android Studio 4.1 更新內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android Studio打包.so庫到apk中實(shí)例詳解
這篇文章主要介紹了Android Studio打包.so庫到apk中實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04