kotlin 如何从AppNavBar中删除BACK箭头?

cwxwcias  于 2023-01-02  发布在  Kotlin
关注(0)|答案(1)|浏览(81)

我在StackOverflow上读到过一些关于这一点的解释,但没有什么帮助。我所做的是使用Android Studio创建一个Navigation Drawer Activity示例应用,它具有hamburger导航图标和滑动导航抽屉功能。它运行得很好!
然而,当我将示例应用程序的每个方面都复制到我的应用程序中时,从MainActivityAppManifestNavigation Maptheme设置再到layout XML,我仍然得到一个血腥的后退箭头和片段标题。
到底是什么代码在控制它?

    • 这就是我需要的:**
    • 这就是我得到的**

[

]
我担心使用数据绑定可能会破坏AppNavBar设置的某些关联。

    • 这是我的主活动. xml**
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            android:id="@+id/app_bar_main"
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <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:headerLayout="@layout/nav_header_main"
            app:menu="@menu/sc_drawer_menu_items" />
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

这是我的MainActivity类。

open class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: MainActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.main_activity)

        setSupportActionBar(binding.appBarMain.toolbar)

        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_main) as NavHostFragment
        val navController = navHostFragment.navController

        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.sc_menu_i_inv_count,
                R.id.sc_menu_i_work_orders,
                R.id.sc_menu_i_ins_queue,
                R.id.sc_menu_i_return_queue,
                R.id.sc_menu_i_settings,
                R.id.sc_menu_i_sign_out,
                R.id.sc_menu_image_list_view
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main_activity, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

除了对DataBindingUtils进行了一些更改之外,它应该执行与我所理解的相同的操作。

ee7vknir

ee7vknir1#

找到了解决方案。将此代码添加到我的MainActivity.kt

val drawerLayout: DrawerLayout = binding.drawerLayout
    val navView: NavigationView = binding.navView
    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_main) as NavHostFragment
    val navController = navHostFragment.navController
    getSupportActionBar()?.setDisplayHomeAsUpEnabled(true)
    getSupportActionBar()?.setHomeButtonEnabled(true)
    appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.sc_menu_i_inv_count,
            R.id.sc_menu_i_work_orders,
            R.id.sc_menu_i_ins_queue,
            R.id.sc_menu_i_return_queue,
            R.id.sc_menu_i_settings,
            R.id.sc_menu_i_sign_out,
            R.id.sc_menu_image_list_view
        ), drawerLayout
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

    actionBarDrawerToggle =
        ActionBarDrawerToggle(this,
            drawerLayout,
            binding.appBarMain.toolbar,
            R.string.nav_open,
            R.string.nav_close)
    drawerLayout.addDrawerListener(actionBarDrawerToggle)
    actionBarDrawerToggle.syncState()
    supportActionBar!!.setDisplayHomeAsUpEnabled(true)

相关问题