当我打开我的应用程序并点击汉堡图标时,它不会打开导航抽屉。但当我从边缘滑动时,它就打开了。此外,抽屉在滑动动作之后响应汉堡图标点击事件。
我在stackoverflow上尝试了各种解决方案,但都无法解决问题。请有人帮忙。提前谢谢。
XML代码:
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="Nexzen"
app:titleTextColor="@color/white"
android:background="@color/dark_green"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabLayout"
android:background="@color/dark_green"
app:tabTextColor="@color/white"
app:tabIndicator="@color/receive_message"
app:tabSelectedTextColor="@color/receive_message"
android:layout_below="@id/appBarLayout"/>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
android:layout_below="@id/tabLayout"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navmenu"
android:visibility="gone"
app:menu="@menu/drawer_menu"
app:itemTextColor="@color/text_color"
app:itemIconTint="@color/text_color"
app:headerLayout="@layout/nav_header"
android:layout_gravity = "start" />
</androidx.drawerlayout.widget.DrawerLayout>
活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
toggle = new ActionBarDrawerToggle(this, binding.drawer, binding.toolbar, R.string.open, R.string.close);
toggle.getDrawerArrowDrawable().setColor(getColor(R.color.white));
toggle.setDrawerIndicatorEnabled(true);
toggle.setDrawerSlideAnimationEnabled(true);
binding.drawer.addDrawerListener(toggle);
toggle.syncState();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
binding.drawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
确保您没有将片段中NavigationView的可见性设置为不可见或消失。这就是我的原因。
滑动必须使导航抽屉可见。点击汉堡包按钮不会。