網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了android實現(xiàn)底部導航欄的具體代碼,供大家參考,具體內(nèi)容如下
常見的底部導航欄
動態(tài)效果
實現(xiàn)步驟
1.底部導航欄樣式
我們應該在項目的res文件夾下新建一個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="關注" /> ? ? <item ? ? ? ? android:id="@+id/navigation_user" ? ? ? ? android:icon="@drawable/user_0" ? ? ? ? android:title="我的" /> </menu>
2.新建四個fragment組件
每一個fragment的組件內(nèi)容相同
四個fragement對應的layout
四個fragment布局文件的內(nèi)容也相同,寫上內(nèi)容以區(qū)別是哪個頁面
<?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布局文件(至關重要)
這個文件指定了頁面上顯式那些fragment組件
在項目res下新建一個文件夾專門用來存放此文件
id取值一定要與底部導航欄樣式里面指定的ID相同,因為android自動根據(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"> ?? ? ?? ?<!-- 底部導航欄 --> ? ? <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);
? ? ? ? // 獲取頁面上的底部導航欄控件
? ? ? ? BottomNavigationView navView = findViewById(R.id.nav_view);
? ? ? ? // 配置navigation與底部菜單之間的聯(lián)系
? ? ? ? // 底部菜單的樣式里面的item里面的ID與navigation布局里面指定的ID必須相同,否則會出現(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容器的控制器,這個容器就是頁面的上的fragment容器
? ? ? ? NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
? ? ? ??
? ? ? ? // 啟動
? ? ? ? NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
? ? ? ? NavigationUI.setupWithNavController(navView, navController);
? ? }
}
原文鏈接:https://blog.csdn.net/qq_42418169/article/details/117305196
相關推薦
- 2024-01-15 IDEA 常量字符串過長問題
- 2022-11-10 Android?Jetpack組件支持庫DataBinding與ViewModel與LiveData
- 2022-03-23 C++通過文件指針獲取文件大小的方法實現(xiàn)_C 語言
- 2022-05-26 Pytorch中使用ImageFolder讀取數(shù)據(jù)集時忽略特定文件_python
- 2023-06-02 Pandas計算元素的數(shù)量和頻率的方法(出現(xiàn)的次數(shù))_python
- 2022-12-13 C++實現(xiàn)一個簡易版的事件(Event)的示例代碼_C 語言
- 2021-12-02 docker容器時區(qū)錯誤問題_docker
- 2023-02-05 通過示例學習python中os模塊的使用_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支