我有两个使用抽屉布局的布局,并且它们对NavigationView使用相同的代码,问题是它们之一更改了所选项目的颜色,而另一个却没有改变,即使它们是相同的精确代码。这是xml代码:
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view_passager"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/menu_passager" />
和Java用于第一种布局:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.acceuil_passager_item:
toolbar.setTitle("Accueil");
fm.beginTransaction().replace(R.id.frame_passager, new AcceuilPassagerFragment()).commit();
break;
case R.id.profile_item:
toolbar.setTitle("Profil");
fm.beginTransaction().replace(R.id.frame_passager, new PassagerProfileFragment()).commit();
break;
case R.id.historique_voyages_item_pass:
toolbar.setTitle("Historique des voyages");
fm.beginTransaction().replace(R.id.frame, new ListeTrajetsFragment()).commit();
break;
case R.id.futurs_voyages_item_pass:
toolbar.setTitle("Futurs voyages");
fm.beginTransaction().replace(R.id.frame_passager, new FutursVoyagesFragment()).commit();
break;
case R.id.log_out_item_pass:
Intent intent = new Intent(PassagerActivity.this, LoginActivity.class);
startActivity(intent);
mAuth.signOut();
finish();
Log.d(TAG, "onNavigationItemSelected: " + (mAuth == null));
break;
default:
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
和第二个布局的Java代码:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.acceuil_item_conducteur:
setUpToolbar(item);
fm.beginTransaction().replace(R.id.frame_conducteur, new AcceuilConducteurFragment()).commit();
break;
case R.id.profile_item_cond:
setUpToolbar(item);
fm.beginTransaction().replace(R.id.frame_conducteur, new ConducteurProfileFragment()).commit();
break;
case R.id.historique_voyages_item_cond:
setUpToolbar(item);
fm.beginTransaction().replace(R.id.frame_conducteur, new HistoriqueVoyagesFragment()).commit();
break;
case R.id.log_out_item_cond:
Intent intent = new Intent(ConducteurActivity.this, LoginActivity.class);
startActivity(intent);
mAuth.signOut();
finish();
Log.d(TAG, "onNavigationItemSelected: " + (mAuth == null));
break;
default:
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
ps:在两种布局中均选择了“配置文件”
答案 0 :(得分:0)
对于导航视图,您必须编写一个颜色选择器,例如:nav_view_item_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primary" android:state_checked="true" />
<item android:drawable="@android:color/transparent" />
</selector>
并设置app:itemBackground="@drawable/nav_view_item_background"
对于文本颜色,您必须具有相同的含义。创建一个textColor选择器并设置app:itemTextColor
用于抽屉布局
return true
(在导航项目选择中)更改颜色和选择。
答案 1 :(得分:0)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:id="@+id/menu_grp"
android:checkableBehavior="single">
原来,checkableBehavior =“ single”是使菜单选择项处于选中状态的原因,而其他布局中则不存在