如果没有导航控制器,抽屉布局将无法工作

2024-01-11

为什么我们无法在没有导航/导航控制器的情况下在 Android 中设置抽屉布局?
每当我们想要设置抽屉时,我们都需要一个导航控制器。就像下面这样:

private lateinit var drawerLayout: DrawerLayout

private lateinit var appBarConfiguration : AppBarConfiguration

val navController = this.findNavController(R.id.myNavHostFragment) NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)

但是,如果应用程序没有 Nav_Graph / NavController,该怎么办?
如果应用程序非常简单怎么办?
在这种情况下,我们应该如何在我们的应用程序中设置抽屉。

请指导。

Note:在发布这个问题之前我做了很多功课和分析,但是在所有文档中我看到Drawer Layout需要NavGraph/NavController/Navigation。


现在的方法是使用导航架构组件 https://developer.android.com/guide/navigation/navigation-getting-started为了在您的应用程序中拥有单个活动和多个片段;每个屏幕都可以用一个片段来表示...这是Android studio的默认设置导航抽屉活动模板。 这NavController用于控制此方法中片段之间的导航

但如果你愿意你可以使用DrawerLayout不使用NavController..但在最近的 Android Studio 版本中,没有相应的模板,您必须手动创建它,并手动处理导航、返回堆栈等几乎所有内容。

Example

这是一个轻量级的示例,可以让您开始一个更大的示例。

当您从抽屉中选择一个项目时,通常您可以在抽屉中进行碎片交易NavigationItemSelectedListener但在下面的例子中我只显示了一个Toast

活动:


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Setting custom ActionBar
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        // Showing the burger button on the ActionBar
        supportActionBar?.setDisplayHomeAsUpEnabled(true); 
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val toggle =
            ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        // Handle navigation click events
        val navigationView: NavigationView = findViewById(R.id.nav_view)
        navigationView.setNavigationItemSelectedListener { item ->

            when (item.itemId) {
                R.id.nav_account -> {
                    Toast.makeText(this, "Account", Toast.LENGTH_SHORT).show()
                }
                R.id.nav_settings -> {
                    Toast.makeText(this, "Setting", Toast.LENGTH_SHORT).show()
                }
                R.id.nav_logout -> {
                    Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show()
                }
            }

            // Closing navigation drawer
            drawerLayout.closeDrawer(GravityCompat.START)

            true
        }
    }
}

活动主文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="28sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <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"
        app:headerLayout="@layout/navigation_header_layout"
        app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

导航_标题_布局.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:gravity="center"
    android:layout_centerInParent="true"
    android:background="@color/colorPrimary"
    android:text="Navigation drawer"
    android:textColor="#ffffff"
    android:textSize="24sp" />

导航菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_account"
        android:title="My Account" />
    <item
        android:id="@+id/nav_settings"
        android:title="Settings" />

    <item
        android:id="@+id/nav_logout"
        android:title="Log Out" />
</menu>

构建.gradle(模块)

 implementation 'com.google.android.material:material:1.2.1'

字符串.xml

<resources>
    <string name="app_name">Navigation Drawer Example</string>
    <string name="open">Open</string>
    <string name="close">Close</string>
</resources>

styles.xml(NoActionBar 主题)

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果没有导航控制器,抽屉布局将无法工作 的相关文章

随机推荐