java - 导航抽屉按钮不起作用
问题描述
我的导航抽屉出现问题,由于某种原因按钮无法正常工作。每次我单击任何按钮时,导航抽屉都会关闭。这是我下面的代码,请帮助。一个例子是当我点击与 nav.homee 关联的按钮时没有创建 toast 并且抽屉只是关闭
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_homee, R.id.nav_gallery, R.id.nav_slideshow,R.id.nav_home)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main14, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id=item.getItemId();
if (id==R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item){
int id=item.getItemId();
if (id==R.id.nav_home){
}else if (id==R.id.nav_gallery){
String link="Check out the best taxi app in Zed! https://play.google.com/store/apps/details?id=com.chomba.haroldking.uta";
Intent share=new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, link);
startActivity(Intent.createChooser(share,"UTA share"));
return true;
}else if (id==R.id.nav_homee){
Toast toast=Toast.makeText(getApplicationContext(),"UTA standard is still under",Toast.LENGTH_LONG);
View view1=toast.getView();
view1.setBackgroundResource(R.color.mdtp_transparent_black);
TextView text=(TextView)view1.findViewById(android.R.id.message);
text.setTextColor(Color.parseColor("#CDEAF7"));
toast.show();
return true;
// Intent intent1 = new Intent(Main14Activity.this, Standarduta.class);
//startActivity(intent1);
}else if (id==R.id.nav_slideshow){
Intent intent1 = new Intent(Main14Activity.this, Main7Activity.class);
startActivity(intent1);
return true;
}
DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
下面是我的 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main14"
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"
app:headerLayout="@layout/nav_header_main14"
app:menu="@menu/activity_main14_drawer" />
</android.support.v4.widget.DrawerLayout>
解决方案
推荐阅读
- c# - Roslyn c# 编译器:非托管结构被检测为可为空,但我想知道这是否是一个悖论
- css - MuiCssBaseline 用不透明度覆盖背景图像
- python - if == 条件未在 django 模板中输出预期结果
- python - Django - 将复选框输入保存到数据库并更新页面
- angular - 同时在同一工作区上构建 Angular 库和应用程序
- node.js - 用于 Node 的 Typescript 库作为 NPM 中的混合包
- python - 在我的本地机器上安装 pyobjc-framework-Quartz 6.2.2 失败
- android - 无法使用 React Native 应用程序将图像上传到 Django 服务器
- python - Python中的列表排序算法不运行
- laravel - 设置 laravel 项目