【问题标题】:How to change text color of Navigation Drawer menu when selected or pressed?选择或按下时如何更改导航抽屉菜单的文本颜色?
【发布时间】:2015-07-03 08:32:32
【问题描述】:

我想改变导航抽屉被点击时的文本颜色。

我该怎么做?

这是我的清单:

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

【问题讨论】:

    标签: android menu colors


    【解决方案1】:

    您可以在代码中使用列表中的 itemclicklistener 或使用状态可绘制资源文件来实现

    【讨论】:

    • 是的,但是怎么做?展示一个示例或将您的“答案”更改为评论。
    【解决方案2】:

    您可以采用如下线性布局:

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

    以及可绘制文件夹中的 row_highlighter:

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

    和 values 文件夹中 colors.xml 中的“父级”,您想要的颜色为十六进制:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="parent">#90caf9</color>
    </resources>
    

    【讨论】:

    • Chichiani 非常感谢。
    【解决方案3】:
    • 您需要使用
      初始化要使用的颜色 ColorStateList 对象。

    • 在为每个状态设置颜色后,您可以设置 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

    示例: 在主类的 onCreate 方法中使用它,只需为状态设置适当的颜色,使用 this official developer documentation 作为状态列表

    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
         */
    

    【讨论】:

      【解决方案4】:

      已经很晚了,但也许可以帮助别人。要更改文本颜色,只需将 android:textColor 链接到 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" />
      

      这是可绘制目录中的 menu_item_color_selector.xml:

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

      【讨论】:

        猜你喜欢
        • 2015-11-09
        • 2016-06-16
        • 2015-09-02
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-08-30
        • 1970-01-01
        相关资源
        最近更新 更多