本文介绍了没有导航控制器,抽屉布局不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不能在没有导航/导航控制器的情况下在 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 会怎样.
如果应用程序很简单怎么办.
在这种情况下,我们应该如何在我们的应用程序中设置一个抽屉.

请指导.

注意:在发布这个问题之前,我做了很多工作和分析,但在所有文档中我看到抽屉布局需要 NavGraph/NavController/Navigation.

解决方案

现在的做法是使用 导航架构组件,以便在您的应用程序中拥有单个活动和多个片段;每个屏幕都可以由一个片段表示...这是 Android Studio Navigation Drawer Activity 模板的默认设置.NavController 用于控制这种方法中片段之间的导航

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

示例

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

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

活动:

类 MainActivity : AppCompatActivity() {覆盖 fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//设置自定义ActionBarval 工具栏:Toolbar = findViewById(R.id.toolbar)setSupportActionBar(工具栏)//在 ActionBar 上显示汉堡按钮supportActionBar?.setDisplayHomeAsUpEnabled(true);val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)val 切换 =ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)drawerLayout.addDrawerListener(切换)切换.syncState()//处理导航点击事件val navigationView: NavigationView = findViewById(R.id.nav_view)navigationView.setNavigationItemSelectedListener { item ->当(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()}}//关闭导航抽屉drawerLayout.closeDrawer(GravityCompat.START)真的}}}

activity_main.

<androidx.appcompat.widget.Toolbarandroid:id="@+id/工具栏";android:layout_width=match_parent"android:layout_height="wrap_content";android:background="@color/colorPrimary";android:theme="@style/ThemeOverlay.AppCompat.Dark";app:layout_constraintTop_toTopOf=父"/><文本视图android:layout_width="wrap_content";android:layout_height="wrap_content";android:text="Hello World!";android:textSize="28sp";app:layout_constraintBottom_toBottomOf=父级"app:layout_constraintLeft_toLeftOf=父"app:layout_constraintRight_toRightOf=父"app:layout_constraintTop_toTopOf=父"/></androidx.constraintlayout.widget.ConstraintLayout><com.google.android.material.navigation.NavigationViewandroid:id="@+id/nav_view";android:layout_width="wrap_content";android:layout_height=match_parent"android:layout_gravity=开始"app:headerLayout="@layout/navigation_header_layout";app:menu="@menu/navigation_menu";/></androidx.drawerlayout.widget.DrawerLayout>

navigation_header_layout.

navigation_menu.

build.gradle(模块)

 实现 'com.google.android.material:material:1.2.1'

strings.

<string name="app_name">导航抽屉示例</string><string name="open">打开</string><string name="close">Close</string></资源>

styles.

<style name="AppTheme";parent="Theme.AppCompat.Light.NoActionBar"><项目名称=colorPrimary">@color/colorPrimary</item><项目名称=colorPrimaryDark">@color/colorPrimaryDark</item><项目名称=colorAccent">@color/colorAccent</item></风格></资源>

Why we cannot setup Drawer Layout in Android without Navigation / Nav-Controller ?
Whenever we want to set up a drawer we need a Nav Controller. Like below:

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)

However, what if an application does not have Nav_Graph / NavController.
What if the application is very simple.
In that case, how do we should setup a Drawer in our App.

Please guide.

Note: Before posting this question I did a lot of homweork and analysis, but in all documentation I saw that Drawer Layout needs NavGraph/NavController/Navigation.

解决方案

The approach now is to use navigation architecture components in order to have a single activity in your app and multiple fragments; each screen can be represented by a fragment...This is the default for the Android studio Navigation Drawer Activity template.The NavController is used to control the navigation between fragments in this approach

But if you wish you can use a DrawerLayout without using NavController.. but in recent Android Studio versions, there is no templates for that and you have to create it manually, and handle navigation, back stack, almost everything manually.

Example

This is a lightweight example that can make you kick off a bigger one.

When you select an item from the drawer, normally you can make fragment transactions in the NavigationItemSelectedListener but in below example I just show up a 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.

<?

navigation_header_layout.

<?

navigation_menu.

<?

build.gradle (Module)

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

strings.

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

styles.

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

这篇关于没有导航控制器,抽屉布局不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 15:29