【问题标题】:Android 'back' action on Toolbar with Navigation Drawer带有导航抽屉的工具栏上的Android“返回”操作
【发布时间】:2018-09-03 12:34:42
【问题描述】:

请帮忙解决这个问题,我已经转了几个小时了! 我有一个导航抽屉设置和一个工具栏(见下面的代码) 我无法让返回/主页功能工作,因为单击它会打开抽屉。

这是在我的 MainActivity 中,在 OnCreate 期间调用。

 private fun initialiseViews() {

    // Initialise the action bar as the XML toolbar, and set the Title as Dashboard
    setSupportActionBar(toolbar)
    supportActionBar!!.title = "Dashboard"

    // Set-up the NavigationView and its listener
    nav_view.setNavigationItemSelectedListener(this)

    // Sets the hamburger toggle and its actions for the toolbar
    val toggle = ActionBarDrawerToggle(
            this,           //host activity
            drawer_layout,        //Drawer Layout object
            toolbar,              // Toolbar Object
            R.string.nav_open,    //description for accessibility
            R.string.nav_closed   //description for accessibility
    )

    // Set the sync-state and draw-listener to the toggle (hamburger)
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
}

以下是我的片段的 OnCreateView 方法内...

(activity as AppCompatActivity).supportActionBar!!.title = "About"
(activity as MainActivity).supportActionBar!!.setHomeButtonEnabled(true)
(activity as MainActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(true)

导航抽屉工作正常。更改工具栏的标题效果很好。汉堡包图标更改为片段内的后退箭头就好了...... 但是,每次我按下后退箭头时,它都会打开抽屉...有点像抽屉的监听器位于后退箭头上方...

在 OnOptionsItemSelected 中实现任何代码都不会产生任何结果,因为不会调用该方法,因为它只是打开抽屉。

非常感谢有人指出我做错了什么的明显之处。

谢谢。

编辑 main_activity.xml 按要求查看...

<android.support.v4.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"
android:fitsSystemWindows="true"
android:background="@drawable/background_graphic">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorLightOrange"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:menu="@menu/toolbar_list_menu"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/not needed for post" />

    <android.support.v4.view.ViewPager
        android:id="@+id/not needed for post" />

</FrameLayout>

<!-- Navigation Drawer -->
<android.support.design.widget.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"
    app:menu="@menu/navigation_menu" />

【问题讨论】:

  • 您可以编辑您的问题并发布您的 xml。
  • 你的意思是AndroidManifest吗?还是 MainActivity xml?
  • 你的活动..
  • 可以显示navigationItemlistener函数体吗?
  • 在我的 navigationItemSelected 监听器中唯一要做的就是将项目 id 传递给另一个决定打开哪个片段的方法

标签: android android-fragments kotlin android-toolbar navigation-drawer


【解决方案1】:

你有没有尝试使用类似的东西来设置它像后退按钮?

        (activity as MainActivity).supportActionBar!!.setNavigationIcon(R.drawable.back)
        (activity as MainActivity).supportActionBar!!.setNavigationOnClickListener {
            (activity as MainActivity).supportActionBar!!.onBackPressed()
        }

并覆盖您的onBackPressed

@Override
public void onBackPressed() {
    if (sendBackPressToDrawer()) {
        return;
    }

    if (sendBackPressToFragmentOnTop()) {
        return;
    }
    super.onBackPressed();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        return;
    }
    finish();
}

【讨论】:

  • 我得到一个未解决的 .setNagivationIcon 引用。删除它,我没有任何与导航相关的建议
  • 试试这个教程medium.com/@nickskelton/…
  • 你必须在setSupportActionBar(toolbar)之后调用setNavigationIcon
  • 仍在阅读目前看来不错的 medium.com 文章。但刚刚发现我只能在工具栏视图本身上调用 {setNavigationIcon} 。调用 {supportActionBar} 后无法设置
  • 如果您直接通过工具栏而不是 supportActionBar 调用它会发生什么?
猜你喜欢
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多