抽屉布局在没有导航控制器的情况下不起作用

Drawer Layout does not work without Navigation Controller

为什么我们不能在 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 怎么办。
如果应用很简单呢。
那么,我们应该如何在我们的应用程序中设置一个抽屉。

请指导

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

现在的方法是使用 navigation architecture components 以便在您的应用程序中有一个 activity 和多个片段;每个屏幕都可以由一个片段表示...这是 Android studio Navigation Drawer Activity 模板的默认设置。 NavController 用于控制片段之间的导航,这种方法

但是如果你希望你可以使用 DrawerLayout 而不使用 NavController.. 但是在最近的 Android Studio 版本中,没有模板,你必须创建它手动,并处理导航、返回堆栈,几乎所有的事情都是手动的。

例子

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

当你select抽屉里的物品时,通常你可以在NavigationItemSelectedListener中进行片段交易,但在下面的例子中我只显示了一个Toast

Activity:


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
        }
    }
}

activity_main.xml

<?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>

navigation_header_layout.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" />

navigation_menu.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>

build.gradle(模块)

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

strings.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>