借助Android Studio
新建項(xiàng)目時(shí)的Bottom Navigation Activity
模板以及官方文檔實(shí)現(xiàn)BottomNavigationView
的簡單使用佛致。
簡單使用
-
首先引入
Navigation
:dependencies { ... //navigation implementation 'androidx.navigation:navigation-fragment:2.2.0' implementation 'androidx.navigation:navigation-ui:2.2.0' }
-
創(chuàng)建對應(yīng)的
navigation
文件nav_graph.xml
:<?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/nav_graph" app:startDestination="@id/homeFragment"> <fragment android:id="@+id/navigation_home" android:name="com.example.bottomnavigationtest.ui.HomeFragment" android:label="HomeFragment" tools:layout="@layout/fragment_test" /> <fragment android:id="@+id/navigation_dashboard" android:name="com.example.bottomnavigationtest.ui.DashboardFragment" android:label="DashboardFragment" /> <fragment android:id="@+id/navigation_notifications" android:name="com.example.bottomnavigationtest.ui.NotificationFragment" android:label="NotificationFragment" /> </navigation>
-
創(chuàng)建底部導(dǎo)航欄按鈕文件
bottom_nav_menu.xml
陌凳,注意這里的id要與nav_graph.xml
中對應(yīng)的fragment
一致。<?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/ic_home_black_24dp" android:title="Home" /> <item android:id="@+id/navigation_dashboard" android:icon="@drawable/ic_dashboard_black_24dp" android:title="Dashboard" /> <item android:id="@+id/navigation_notifications" android:icon="@drawable/ic_notifications_black_24dp" android:title="Notifications" /> </menu>
-
創(chuàng)建相應(yīng)
MainActivity
布局activity_main.xml
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- app:navGraph="@navigation/nav_graph" 關(guān)聯(lián)navigation導(dǎo)航 --> <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/nav_graph" /> <!-- app:menu="@menu/bottom_nav_menu" 關(guān)聯(lián)底部導(dǎo)航item --> <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" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
將
Navigayion
與NavController
相關(guān)聯(lián)public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //BottomNavigationView BottomNavigationView navigationView = findViewById(R.id.nav_view); //構(gòu)建導(dǎo)航id menu 與 navigation中的id要相對應(yīng) AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) .build(); final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); //設(shè)置ActionBar標(biāo)題與當(dāng)前所顯示的fragment相對應(yīng) NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); //與navcontroller相關(guān)聯(lián) NavigationUI.setupWithNavController(navigationView, navController); //設(shè)置BottomNavigationView點(diǎn)擊事件 navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { //跳轉(zhuǎn)相應(yīng)fragment navController.navigate(menuItem.getItemId()); //返回false會(huì)有一個(gè)點(diǎn)擊懸浮效果,返回true則不會(huì)有該效果 return false; } }); } }