<item
android:id="@id/donutList"
android:icon="@drawable/donut_with_sprinkles"
android:title="@string/donut_name" />
<item
android:id="@id/coffeeList"
android:icon="@drawable/coffee_cup"
android:title="@string/coffee_name" />
现在 MenuItem
已经就绪,我在 mainActivity
的布局中添加了 BottomNavigationView
,并且将 bottom_nav_menu
设置为 BottomNavigationView
的 menu
属性。
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
app:menu="@menu/bottom_nav_menu" />
要使底部标签栏发挥作用,这里调用 setupWithNavController()
函数将 navController
传入 BottomNavigationView
。
private fun setupBottomNavMenu(navController: NavController) {
val bottomNav = findViewById(
R.id.bottom_nav_view
)
bottomNav?.setupWithNavController(navController)
}
请注意我并没有从导航图中调用任何导航操作。实际上导航图中甚至没有前往 coffeeList
Fragment 的路径。和之前对 ActionBar
所做的操作一样,BottomNavigationView
通过匹配 MenuItem
的 id
和导航目的页面的 id
来自动响应导航操作。
抽屉式导航栏
虽然看上去不错,但是如果您设备的屏幕尺寸较大,那么底部标签栏恐怕无法提供最佳的用户体验。要解决这个问题,我会使用另外一个布局文件,它带有 w960dp 限定符,表明它适用于屏幕更大、更宽的设备。
这个布局文件与默认的 activity_main
布局相类似,其中已经包含了 Toolbar
和 FragmentContainerView
。我需要添加 NavigationView
,并且将 nav_drawer_menu
设置为 NavigationView
的 menu
属性。接下来,我将在 NavigationView
和 FragmentContainerView
之间添加分隔符。
<RelativeLayout
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=“com.android.samples.donuttracker.MainActivity”>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:layout_alignParentStart=“true”
app:elevation=“0dp”
app:menu="@menu/nav_drawer_menu" />
<View
android:layout_width=“1dp”
android:layout_height=“match_parent”
android:layout_toEndOf="@id/nav_view"
android:background="?android:attr/listDivider" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:background="@color/colorPrimary"
android:layout_toEndOf="@id/nav_view"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name=“androidx.navigation.fragment.NavHostFragment”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_below="@id/toolbar"
app:defaultNavHost=“true”
android:layout_toEndOf="@id/nav_view"
app:navGraph="@navigation/nav_graph" />
如此一来,在宽屏幕设备上,NavigationView
会代替 BottomNavigationView
显示在屏幕上。现在布局文件已经就绪,我再创建一个 nav_drawer_menu.xml
,并且将 donutList
和 coffeeList
作为主要的分组添加为目的页面。对于 MenuItem
,我添加了 selectionFragment
作为它的目的页面。
<item
android:id="@id/donutList"
android:icon="@drawable/donut_with_sprinkles"
android:title="@string/donut_name" />
<item
android:id="@id/coffeeList"
android:icon="@drawable/coffee_cup"
android:title="@string/coffee_name" />
<item
android:id="@+id/selectionFragment"
android:title="@string/action_settings" />
现在所有布局已经就绪,我们回到 MainActivity
,设置抽屉式导航栏,使其能够与 NavigationController
协作。和之前针对 BottomNavigationView
所做的相类似,这里创建一个新的方法,并且调用 setupWithNavController()
函数将 navController
传入 NavigationView
。为了使代码保持整洁、各个元素之间更加清晰,我们会在新的方法中实现相关操作,并且在 onCreate()
中调用该方法。
private fun setupNavigationMenu(navController: NavController){
val sideNavView = findViewById(R.id.nav_view)
sideNavView?.setupWithNavController(navController)
}
现在当我在屏幕较宽的设备上运行应用时,可以看到抽屉式导航栏已经设置了 MenuItem,并且在导航图中,MenuItem 和目的页面的 id 是相匹配的。
△ 在屏幕较宽的设备上运行 Donut Tracker
总结
最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!
这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。
相信它会给大家带来很多收获:
上述【高清技术脑图】以及【配套的架构技术PDF】可以关注我免费获取
及【配套的架构技术PDF】可以关注我免费获取**
当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。