英文:
Navigation drawer buttons are not working
问题
我遇到了一个导航抽屉的问题,出现了一些按钮无法正常工作的情况。每当我点击任何按钮时,导航抽屉都会简单地关闭。以下是我的代码,请帮忙看看。例如,当我点击与 nav.homee
相关的按钮时,不会创建任何提示,抽屉只是关闭了。
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) {
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;
} 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>
如果您需要更多的帮助,请随时提问。
英文:
I am experiencing a problem with my navigation drawer, for some reason the buttons are not working. Every time I click on any button the navigation drawer simply closes. This is my code below, please help. An example is when I click on the button associated with nav.homee No toast is created and the drawer simply closes
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();
}
}
Below is my 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>
专注分享java语言的经验与见解,让所有开发者获益!
评论