我是 android 的初学者开发人员,我发现构建以下应用程序非常困难。
我想创建一个可以同时使用底部导航视图和抽屉布局的应用程序。我成功了,因为这两个项目都正常工作(我正在使用 NavigatioUI 实用程序)。我的问题是关于向上按钮。当我在两个导航视图之一上单击一个项目后更改片段时,向上按钮转换为后退箭头,因此如果我单击它,我将返回默认片段。我希望能够从每个片段中打开抽屉菜单,但我不能!谁能帮帮我吗?谢谢!
这是 MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//setto la toolbar come Action Bar
setSupportActionBar(toolbar2)
//inizializzo gli elementi di navigation
setupNavigation()
}
/*
settaggio degli elementi di Navigation.
Il NavigationUI recupera l'ID selezionato col bottone e lo collega al fragment dentro il mobile_navigation
*/
private fun setupNavigation() {
//per usare la libreria Navigation per viaggiare tra i vari fragment serve un Navigation Controller
val navController = Navigation.findNavController(this, R.id.nav_host_fragment2)
//settaggio Toolbar per avere il bottone a sinistra (drawer layout)
NavigationUI.setupActionBarWithNavController(this, navController, drawer_layout2)
//settaggio del Bottom Navigation
bottom_nav2?.let {
NavigationUI.setupWithNavController(it, navController)
}
//settaggio del Drawer Layout
nav_view2?.let {
NavigationUI.setupWithNavController(it, navController)
}
}
//popola la Toolbar con i suoi item menu
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_toolbar, menu)
return true
}
//funzione per gestire il click sugli elementi della toolbar presi dal menu
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val navController = Navigation.findNavController(this, R.id.nav_host_fragment2)
val navigated = NavigationUI.onNavDestinationSelected(item!!, navController)
return navigated || super.onOptionsItemSelected(item)
}
//questa funzione consente di attivare il click sull'icona in alto a sinistra qualunque sia
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(Navigation.findNavController(this, R.id.nav_host_fragment2), drawer_layout2)
}
}
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary" />
<fragment
android:id="@+id/nav_host_fragment2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom_nav2"
app:menu="@menu/menu_navigation"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>