我看过有关此问题的其他文章,但似乎无法解决,所以我希望这只是我遗漏了一些明显情况的情况。
这是我的代码: MainActivity.XML
<androidx.drawerlayout.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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/translation_panel_blue_open_mea"
android:minHeight="57dp"
android:padding="1dp"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextColor="@color/header_grey_mea"
app:titleTextColor="@color/header_grey_mea"
android:theme="@style/AppTheme">
</androidx.appcompat.widget.Toolbar>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme"
app:menu="@menu/burger_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
burger_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/menuLogout"
android:onClick="initUserLogout"
android:title="@string/log_out">
</item>
<item
android:id="@+id/menuItemTutorial"
android:onClick="launchTutorialView"
android:title="@string/tutorial">
</item>
</group>
</menu>
AppTheme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
和我在initBurgerMenu
中运行的MainActivity
函数:
private fun initBurgerMenu(){
mDrawerLayout = findViewById(R.id.drawer_layout)
val navigationView: NavigationView = findViewById(R.id.nav_view)
navigationView.setNavigationItemSelectedListener { menuItem ->
// set item as selected to persist highlight
menuItem.isChecked = true
// close drawer when item is tapped
mDrawerLayout.closeDrawers()
when (menuItem.itemId) {
R.id.menuLogout -> {
initUserLogout()
}
R.id.menuItemTutorial -> {
Toast.makeText(this, "tutorial", Toast.LENGTH_LONG).show()
}
}
true
}
}
答案 0 :(得分:1)
像这样关闭您的AppBarLayout标签
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="57dp"
android:padding="1dp"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/AppTheme">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
答案 1 :(得分:0)
您在xml中的导航视图来自com.google.android.material.navigation.NavigationView
,并且您可能正试图从NavigationView
扩展android.support.design.widget
,如果这是问题所在,请删除导入并重新导入正确的包