我是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>
使用AppBarConfiguration声明您的顶级目的地。
按照下面的代码更改
声明
private lateinit var appBarConfiguration: AppBarConfiguration
在您的setupNavigation方法中,
NavigationView navView = findViewById(R.id.nav_view);
//Here include the ids of fragments that you want as top level fragments.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send,R.id.nav_custom
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
您还需要在抽屉式布局中包含NavigationView
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer" />
activity_main_drawer菜单应包含导航抽屉中所需的所有项目。