将底部菜单栏与导航架构组件一起使用时如何从工具栏中删除后退按钮
How to remove back button from toolbar when using bottom menu bar with navigation architecture components
我有一个底部菜单栏的应用程序,用户可以使用它在 4 个主页选项卡之间切换。它工作正常,如下所示。
我遇到的唯一问题是当我在不同片段之间切换时显示后退按钮。由于所有这些片段都处于同一级别,我不希望它表现得那样。
这是我的实现。
MainNavigationActivity
class MainNavigationActivity : AppCompatActivity() {
private lateinit var navigationController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initialization()
}
private fun initialization(){
navigationController = Navigation.findNavController(this, R.id.hostFragment)
setSupportActionBar(toolbar)
NavigationUI.setupWithNavController(bottomNavigationBar,navigationController)
NavigationUI.setupActionBarWithNavController(this,navigationController)
}
override fun onBackPressed() {
onSupportNavigateUp()
}
MainNavigationActivity 布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainNavigationActivity">
<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"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<fragment
android:id="@+id/hostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:layout_above="@id/bottomNavigationBar"
app:defaultNavHost="true"
app:navGraph="@navigation/main_navigation_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
app:menu="@menu/bottom_navigation_menu"
app:labelVisibilityMode="labeled"/>
</RelativeLayout>
bottom_navigation_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:state_checked="true"
android:color="@color/colorPrimary"
android:title="@string/navigation_home"
android:icon="@drawable/ic_bottom_bar_menu_home"/>
<item
android:id="@+id/navigation_offers"
android:state_checked="false"
android:color="@color/gray"
android:title="@string/navigation_offers"
android:icon="@drawable/ic_bottom_bar_menu_offers"/>
<item
android:id="@+id/navigation_my_bookings"
android:state_checked="false"
android:color="@color/gray"
android:title="@string/navigation_my_bookings"
android:icon="@drawable/ic_bottom_bar_menu_bookings"/>
<item
android:id="@+id/navigation_my_account"
android:state_checked="false"
android:color="@color/gray"
android:title="@string/navigation_my_account"
android:icon="@drawable/ic_bottom_bar_menu_account"/>
</menu>
导航图中的片段的ID与menu.xml中的id相同。这就是它识别正确片段并正确切换到该片段的方式。
如何删除主屏幕级别工具栏上的这个后退按钮?
根据the NavigationUI
documentation:
By default, the Navigation button is hidden when a user is at a top-level destination of a navigation graph and appears as an Up button in any other destination.
If you want to customize which destinations are considered top-level destinations, you can instead pass a set of destination IDs to the constructor, as shown below:
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_offers,
R.id.navigation_my_bookings, R.id.navigation_my_account))
(请注意,此构造函数需要 navigation-ui-ktx
工件 - 替代方法是使用 AppBarConfiguration.Builder
)
您还可以使用 AppBarConfiguration
的扩展函数,它将底部导航菜单作为其参数,只是为了让事情变得更容易一些。 Check here
它会将底部导航栏中的所有片段设置为顶级目的地。
就这么简单:
val appBarConfiguration = AppBarConfiguration(bottomNavigationBar.menu)
setupActionBarWithNavController(navigationController, appBarConfiguration)
而且正如@ianhanniballake 所建议的那样,您需要 navigation-ui-ktx
才能正常工作。
我有一个底部菜单栏的应用程序,用户可以使用它在 4 个主页选项卡之间切换。它工作正常,如下所示。
我遇到的唯一问题是当我在不同片段之间切换时显示后退按钮。由于所有这些片段都处于同一级别,我不希望它表现得那样。
这是我的实现。
MainNavigationActivity
class MainNavigationActivity : AppCompatActivity() {
private lateinit var navigationController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initialization()
}
private fun initialization(){
navigationController = Navigation.findNavController(this, R.id.hostFragment)
setSupportActionBar(toolbar)
NavigationUI.setupWithNavController(bottomNavigationBar,navigationController)
NavigationUI.setupActionBarWithNavController(this,navigationController)
}
override fun onBackPressed() {
onSupportNavigateUp()
}
MainNavigationActivity 布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainNavigationActivity">
<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"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<fragment
android:id="@+id/hostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:layout_above="@id/bottomNavigationBar"
app:defaultNavHost="true"
app:navGraph="@navigation/main_navigation_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
app:menu="@menu/bottom_navigation_menu"
app:labelVisibilityMode="labeled"/>
</RelativeLayout>
bottom_navigation_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:state_checked="true"
android:color="@color/colorPrimary"
android:title="@string/navigation_home"
android:icon="@drawable/ic_bottom_bar_menu_home"/>
<item
android:id="@+id/navigation_offers"
android:state_checked="false"
android:color="@color/gray"
android:title="@string/navigation_offers"
android:icon="@drawable/ic_bottom_bar_menu_offers"/>
<item
android:id="@+id/navigation_my_bookings"
android:state_checked="false"
android:color="@color/gray"
android:title="@string/navigation_my_bookings"
android:icon="@drawable/ic_bottom_bar_menu_bookings"/>
<item
android:id="@+id/navigation_my_account"
android:state_checked="false"
android:color="@color/gray"
android:title="@string/navigation_my_account"
android:icon="@drawable/ic_bottom_bar_menu_account"/>
</menu>
导航图中的片段的ID与menu.xml中的id相同。这就是它识别正确片段并正确切换到该片段的方式。
如何删除主屏幕级别工具栏上的这个后退按钮?
根据the NavigationUI
documentation:
By default, the Navigation button is hidden when a user is at a top-level destination of a navigation graph and appears as an Up button in any other destination.
If you want to customize which destinations are considered top-level destinations, you can instead pass a set of destination IDs to the constructor, as shown below:
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_offers,
R.id.navigation_my_bookings, R.id.navigation_my_account))
(请注意,此构造函数需要 navigation-ui-ktx
工件 - 替代方法是使用 AppBarConfiguration.Builder
)
您还可以使用 AppBarConfiguration
的扩展函数,它将底部导航菜单作为其参数,只是为了让事情变得更容易一些。 Check here
它会将底部导航栏中的所有片段设置为顶级目的地。
就这么简单:
val appBarConfiguration = AppBarConfiguration(bottomNavigationBar.menu)
setupActionBarWithNavController(navigationController, appBarConfiguration)
而且正如@ianhanniballake 所建议的那样,您需要 navigation-ui-ktx
才能正常工作。