android - 带有导航抽屉的工具栏上的Android“返回”操作
问题描述
请帮忙解决这个问题,我已经转了几个小时了!我设置了一个导航抽屉和一个工具栏(见下面的代码)我无法让返回/主页功能工作,因为单击它会打开抽屉。
这是在我的 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" />
解决方案
您是否尝试使用类似的东西将其设置为后退按钮?
(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();
}
推荐阅读
- python - 休息 API 。帮助更改 ListView
- javascript - TypeError:“HTMLElement 构造函数:'new' 是必需的”
- python - 从文件中保存和检索变量值
- reactjs - 将 GitHub gist 嵌入 Docusaurus v2 文档
- android - Moshi - 使用动态参数解析 JSON
- windows - AWS ec2 windows 登录错误说发生了身份验证错误。无法联系当地安全机构
- javascript - Discord 单条消息删除器机器人
- php - Symfony Column not found: 1054 Unknown column 't44.id' in 'on Clause'
- python-3.x - 如何将最新 (2020) Django 安装到 AWS EC2 Linux 2 实例并使用 Apache Hello World 服务
- mysql - 当我想在idea上运行我的j2ee+maven+spring-boot+mysql项目时,出现错误