简体   繁体   English

导航查看所选项目的颜色

[英]NavigationView selected item color

i have two layouts that use a drawerLayout and they use the same code for the navigationView, the problem is that one of them changes the color of the selected item while the other doesn't even though it's the same exact code. 我有两个使用抽屉式布局的布局,并且它们对NavigationView使用相同的代码,问题是它们之一更改了所选项目的颜色,而另一个却没有变化,即使它是相同的完全相同的代码。 here is the xml code: 这是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"  />

and java for the 1st layout: 和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;
}

and the java code for the 2nd layout : 和第二个布局的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;
}

and here is the first layout 这是第一个布局

and the 2nd one 和第二个

ps: "Profil" is selected in both layouts ps:在两个布局中均选择了“配置文件”

For navigation view you have to write a color selector like: nav_view_item_background 对于导航视图,您必须编写一个颜色选择器,例如: 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>

and set app:itemBackground="@drawable/nav_view_item_background" 并设置app:itemBackground="@drawable/nav_view_item_background"

For text color you have to same thing. 对于文本颜色,您必须做同样的事情。 Create a textColor selector and set app:itemTextColor 创建一个textColor选择器并设置app:itemTextColor

For Drawer Layout return true (In Navigation item selection) change the color and selection. 对于“抽屉布局”, return true (在“导航”选项中),更改颜色和选择。

<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">

Turned out checkableBehavior = "single" is what made the menu selected item checked and what was missing in my other layout 原来checkableBehavior =“ single”是使菜单选择项处于选中状态的原因,而我的其他布局中缺少的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM