NavigationDrawer不随AndroidX一起出现

时间:2019-05-21 20:20:20

标签: android androidx android-navigation-drawer

我有一个正在与androidx组件一起使用的应用程序,但无法获取显示DrawerActivity的汉堡图标。这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main"
        tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/Widget.MyApp.ActionBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/sliding_tabs"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

    <com.webnation.begonerobotexters.widgets.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:layout_constraintTop_toBottomOf="@+id/toolbar"
            app:layout_constraintBottom_toTopOf="@+id/viewpager"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

    <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/sliding_tabs"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

<!-- The navigation drawer -->
<com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@android:color/white"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>

这是我在活动中设置导航抽屉的方式:

private fun setUpNavigationDrawer() {
    setSupportActionBar(toolbar)
    val actionBar = supportActionBar

    try {
        assert(actionBar != null)
        actionBar?.setDisplayHomeAsUpEnabled(true)
        actionBar?.setHomeButtonEnabled(true)
        //actionBar?.setSubtitle(getString(R.string.subtitle))
        actionBar?.setDisplayShowTitleEnabled(true)
    } catch (ignored: Exception) {
        Timber.e(ignored)
        ignored.printStackTrace()
    }

    toolbar?.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary))
    toolbar?.setTitleTextColor(ContextCompat.getColor(this, R.color.tab_text_color))


    navigation_view.setNavigationItemSelectedListener { menuItem ->
        menuItem.isChecked = true
        when (menuItem.itemId) {
            R.id.navigation_item_1 -> {
                val intent = Intent(this, EulaActivity::class.java)
                intent.putExtra(keyFileName, "privacy")
                startActivity(intent)
            }
            R.id.navigation_item_2 -> {
                val intent = Intent(this, EulaActivity::class.java)
                intent.putExtra(keyFileName, "eula")
                startActivity(intent)
            }
            R.id.navigation_item_3 -> {

            }


        }
        if (navigation_view != null) {
            drawer_layout?.closeDrawer(navigation_view)
        }
        true
    }


    mDrawerToggle = object: ActionBarDrawerToggle(this, drawer_layout, toolbar,R.string.drawer_open, R.string.drawer_close) {
        override fun onDrawerOpened(drawerView: View) {
            super.onDrawerOpened(drawerView)

            invalidateOptionsMenu()
        }

        override fun onDrawerClosed(view: View) {
            super.onDrawerClosed(view)
            //getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu()
        }
    }

    mDrawerToggle.isDrawerIndicatorEnabled = true
    drawer_layout?.addDrawerListener(mDrawerToggle)
    mDrawerToggle.syncState()

}

这是我的DrawActivity:

class DrawerActivity : AppCompatActivity() {

lateinit var draw_layout: DrawerLayout
lateinit var mDrawerToggle: ActionBarDrawerToggle

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
}

override fun onStart() {
    super.onStart()

    draw_layout = findViewById<DrawerLayout>(R.id.drawer_layout)
    mDrawerToggle = ActionBarDrawerToggle(this, draw_layout, 0, 0)
    draw_layout.addDrawerListener(mDrawerToggle)

    val actionBar = supportActionBar
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true)
        actionBar.setDisplayShowHomeEnabled(true)
        actionBar.setDisplayShowTitleEnabled(true)
        actionBar.setDisplayUseLogoEnabled(false)
        actionBar.setHomeButtonEnabled(true)
    }
}

override fun onPostCreate(savedInstanceState: Bundle?) {
    super.onPostCreate(savedInstanceState)
    mDrawerToggle.syncState()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item)
}
}

我在做什么错了?

2 个答案:

答案 0 :(得分:0)

plt.imsave已过时了一段时间;可以为该按钮使用框架自己的resId(被夸大到ActionBarDrawerToggle的自定义视图中-以便将其显示在应有的位置):

ActionBar

一个人可以同时获得click事件和右上角的菜单事件;例如:

<androidx.appcompat.widget.AppCompatImageButton
    android:id="@android:id/home"
    android:src="@drawable/ic_menu_black_36dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@null" />

答案 1 :(得分:0)

我发现viewpager / sliding bar标签页覆盖了操作栏。因此,我将ConstraintLayout换成了LinearLayout,并出现了 poof

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical"
        tools:showIn="@layout/activity_main"
        tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/Widget.MyApp.ActionBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
            />


   <com.webnation.begonerobotexters.widgets.SlidingTabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            />
    <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"

    />

</LinearLayout>

<!-- The navigation drawer -->
<com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@android:color/white"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>