Android NavigationDrawer不会在点击汉堡包图标时切换

时间:2018-04-27 07:34:11

标签: android android-layout kotlin navigation-drawer android-view

我有一种奇怪的情况。下面是我的主要活动代码的片段,其中有一个导航抽屉

open class MainActivity : BaseActivity(),NavigationView.OnNavigationItemSelectedListener {

var toggle : ActionBarDrawerToggle? = null
var mInstance : MainActivity? = null

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setSupportActionBar(toolbar)

    toggle = object : ActionBarDrawerToggle(this,drawer_layout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close){}

    drawer_layout.addDrawerListener(toggle as DrawerLayout.DrawerListener)
    toggle?.isDrawerIndicatorEnabled = true
    toggle?.syncState()

    val back : Drawable = resources.getDrawable(R.drawable.ic_arrow_back)
    supportActionBar?.setHomeAsUpIndicator(back)
    nav_view.setNavigationItemSelectedListener(this)
    showFragment(LandingFragment())
    logout_layout.setOnClickListener({ logout()})
}

问题

单击汉堡图标时导航抽屉不会打开,但是一旦使用滑动手势打开抽屉,点击就会开始工作。可能是什么原因。我们将不胜感激。

修改

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:openDrawer="start">

<include
    layout="@layout/activity_common_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


<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"
    android:background="@color/white"
    app:headerLayout="@layout/nav_header_main"
    android:visibility="gone"
    app:menu="@menu/nav_menu"
    >

    <RelativeLayout
        android:id="@+id/logout_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        >

        <ImageView
            android:id="@+id/iv_logout"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/ic_exit_to_app"
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Logout"
            android:layout_marginLeft="24dp"
            android:layout_centerVertical="true"
            android:textStyle="bold"
            android:layout_toRightOf="@id/iv_logout"
            android:textColor="#000"
            />


    </RelativeLayout>

</android.support.design.widget.NavigationView>

0 个答案:

没有答案