简体   繁体   中英

How to change text color of Navigation Drawer menu when selected or pressed?

I wanna change text color of Navigation Drawer when it's clicked.

How can i do it?

this is my list:

<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:contentDescription="@string/desc_list_item_icon"
    android:src="@drawable/ic_launcher"/>

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingLeft="50dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:textSize="15dp"
    android:textStyle="bold"
    android:gravity="center_vertical" />

在此处输入图片说明

you can take a linear layout like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/row_highlighter"
    android:padding="5dp" >

        <TextView
            android:id="@+id/parentTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="@string/app_name"
            android:textColor="@android:color/black"
            android:textSize="15sp" />


</LinearLayout>

and a row_highlighter in drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/parent" android:state_activated="true"/>
    <item android:drawable="@android:color/white" android:state_pressed="true"/>
    <item android:drawable="@color/parent"/>

</selector>

and "parent" in colors.xml in values folder with color u want in hex:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="parent">#90caf9</color>
</resources>
  • You need to initialize the colors you want to use using the
    ColorStateList object.

  • After setting up the colors for each state you set the navitaionView itemTextColor (navigation.setItemTextColor(yourCustomColorStateList);

This is where I got the answer
Here is another related Stackoverflow questions
Official Android documentation of all the available kind of state

example: Use this inside your onCreate method of your main class just set the colors for the state appropriately use this official developer documentation for the list of states

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    /**
     * start of code configuration for color of text of your Navigation Drawer / Menu based on state
     */
    int[][] state = new int[][] {
            new int[] {-android.R.attr.state_enabled}, // disabled
            new int[] {android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_checked}, // unchecked
            new int[] { android.R.attr.state_pressed}  // pressed

    };

    int[] color = new int[] {
            Color.WHITE,
            Color.WHITE,
            Color.WHITE,
            Color.WHITE
    };

    ColorStateList colorStateList1 = new ColorStateList(state, color);


    // FOR NAVIGATION VIEW ITEM ICON COLOR
    int[][] states = new int[][] {
            new int[] {-android.R.attr.state_enabled}, // disabled
            new int[] {android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_checked}, // unchecked
            new int[] { android.R.attr.state_pressed}  // pressed

    };

    int[] colors = new int[] {
            Color.WHITE,
            Color.WHITE,
            Color.WHITE,
            Color.WHITE
    };
    ColorStateList colorStateList2 = new ColorStateList(states, colors);
    navigationView.setItemTextColor(colorStateList1);
    navigationView.setItemIconTintList(colorStateList2);
    /**
     * end of code configuration for color of text of your Navigation Drawer / Menu based on state
     */

您可以使用列表上的itemclicklistener或状态可绘制资源文件在代码中进行操作

It's late but maybe helps someone else. For changing Text color, just link android:textColor to Selector drawable:

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingLeft="50dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:textSize="15dp"
    android:textStyle="bold"
    android:textColor="@drawable/menu_item_color_selector"
    android:gravity="center_vertical" />

and this is menu_item_color_selector.xml in drawable directory:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/colorPrimary"/>
    <item android:color="@color/navMenuTextColor"/>
</selector>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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