【发布时间】: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