当前位置: 首页 > 知识库问答 >
问题:

带有导航抽屉的Android工具栏上的“后退”操作

章涵蓄
2023-03-14

请帮帮我,我已经绕了几个小时了!我设置了一个抽屉菜单和一个工具栏(见下面的代码),我无法让返回/回家功能工作,因为单击它会导致打开抽屉。

这是我的主要活动,在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)

抽屉菜单工作正常。更改工具栏的标题工作正常。汉堡包图标在片段中更改为向后箭头就好了...然而,每次我按下向后箭头,它都会打开抽屉...有点像抽屉的听众在向后箭头上方...

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

非常感谢有人指出我做错了什么。

谢谢

编辑main_活动。按要求查看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" />

共有1个答案

贺飞
2023-03-14

你们有并没有试着用这样的东西把它设置成后退按钮?

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

并覆盖您的onBackPked

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

    if (sendBackPressToFragmentOnTop()) {
        return;
    }
    super.onBackPressed();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        return;
    }
    finish();
}
 类似资料:
  • 我正试图把导航抽屉放在动作栏上,当它像这个应用程序一样向右滑动时:[已删除] 这是我的主要活动布局: 关于stackoverflow的其他一些问题与此类似,但建议所有答案都使用滑动菜单库。但这个应用程序他们仍然使用Android。支持v4.widget。抽屉式布局,他们成功了。不要问我怎么知道他们使用标准的导航抽屉,但我肯定。 非常感谢你的帮助。 这是最终的解决方案:多亏了@Peter Cai,这

  • 不久前,我有一个导航抽屉在工作,但后来我决定将其更新为使用工具栏,而不是默认的支持操作栏。现在,当我点击汉堡包菜单时,导航抽屉没有打开。然而,我可以从左向右滑动并打开它。问题是,它现在是空的。 值-v21/样式。xml 导航U抽屉。xml ctivity.java 我知道NavDrawerItem看起来有点不稳定,但我已经验证了它可以与旧的导航抽屉(前工具栏)一起工作,它真正做的是用图标填充列表视

  • 问题内容: 我在使用Navigation Drawer时遇到问题,它太慢了,我要寻找的解决方案是先关闭抽屉,然后显示活动,但它不起作用,肯定是我遗漏了一些东西。 问题答案: 您可以通过这种方式来避免抽屉滞后,更改 onItemClick : 编辑: 首选方式应该是在DrawerLayout上设置DrawerListener,然后 像这样设置片段:

  • 我实现了双抽屉布局。我还建立了一个ActionBarDrawerToggle。 我的问题是当我点击主页按钮时,我的左抽屉打开并播放动画。打开时我的右抽屉也显示抽屉打开动画。这会导致与主页图标奇怪的同步问题,即使使用mDrawerToggle.sync状态(); 我要做的是拦截打开左侧抽屉的home button click事件,以便测试哪个抽屉打开,然后适当地关闭那个抽屉。有没有办法在主页图标上设

  • 我正在开发一个android项目,我正在尝试使用来自http://developer.android.com/training/implementing-navigation/nav-drawer.html.的示例集成新的导航抽屉 它主要工作除了一件事,动作栏向上按钮不显示菜单,但如果我用手指从边缘滑动主活动屏幕,菜单就会出现,所以我知道实际菜单没有问题,它只是动作栏按钮。 下面是代码 感谢您提供

  • 请给我解释一下。。。我的,它与同步(比如)。活动只有很少的片段,在不同的片段中,我需要使用不同的模式(一种模式是视差,另一种模式是简单)。因此,我认为我应该在每个框架中用AppBar和内容设置协调布局 但我如何替换“新建”上的最后一个工具栏以保存与抽屉的同步?或者这是错误的方式,我需要做一些其他的?