2015-11-14 45 views
1

我正在尝试更改活动主抽屉的文本颜色,但无法找到要更改它的位置。我想将其更改为较亮的颜色(白色)如何更改Android中活动主抽屉的文本颜色

enter image description here

这是什么在我的activity_main_drawer.xml的

<?xml version="1.0" encoding="utf-8"?> 

<group android:checkableBehavior="single"> 
    <item android:id="@+id/nav_city1" android:icon="@android:drawable/ic_menu_mapmode" 
     android:title="Label1" /> 
    <item android:id="@+id/nav_city2" android:icon="@android:drawable/ic_menu_compass" 
     android:title="Label2" /> 
    <item android:id="@+id/nav_city3" android:icon="@android:drawable/ic_menu_myplaces" 
     android:title="Label3" /> 
    <item android:id="@+id/nav_city4" android:icon="@android:drawable/ic_input_get" 
     android:title="Label4" /> 
</group> 

<item android:title="Account"> 
    <menu> 
     <item android:id="@+id/nav_profile" android:icon="@android:drawable/ic_menu_edit" 
      android:title="Profile" /> 
     <item android:id="@+id/nav_settings" android:icon="@android:drawable/ic_menu_manage" 
      android:title="Settings" /> 
    </menu> 
</item> 

我试着插入android:textColor无处不在,但它不工作。

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
android:textColor="#FFFFFF"> 

<group android:checkableBehavior="single" android:textColor="#FFFFFF"> 
    <item android:id="@+id/nav_cities" android:icon="@android:drawable/ic_menu_mapmode" 
     android:title="Cities" android:textColor="#FFFFFF"/> 
+0

显示你的颜色XML如果你have..and抽屉XML –

+0

检查此http的列表项:/ /堆栈溢出。com/questions/22192291 /如何更改状态栏中的颜色android – Destro

+0

@TwoThumbSticks是否使用了android设计支持库中提供的** NavigationView **来实现导航抽屉? – Droidwala

回答

1
  • 需要初始化要使用
    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方法里面

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

很好解释和代码作品像一个魅力谢谢你! – TwoThumbSticks

0

看来您的使用是新的Android Support Design Library。我没有用过它。 我觉得你需要有一些标题布局它由TextView组成,如上图所示。 稍后在Java代码中,您可以访问标题布局并使用findViewById()来获取TextView

让我知道它是否有效。

2

您可以只使用app:itemTextColor您NavigationView内实现通过XML一样:

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_height="match_parent" 
    android:layout_width="240dp" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    android:background="@color/colorWhite" 
    app:headerLayout="@layout/drawer_header" 
    app:menu="@menu/drawer_view" 
    app:itemTextColor="@color/colorWhite"> 

添加上面一行到你的XML应该在你的情况下工作。
希望它有帮助!

0

可以使用应用:itemTextColor您NavigationVi EW

<?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:background="#FFDFE3" 
 
    android:fitsSystemWindows="true" 
 
    tools:openDrawer="start"> 
 

 
    <include 
 
     layout="@layout/app_bar_main" 
 
     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" 
 
     <!-- here you can change color of items like this. --> 
 
     app:itemTextColor="@color/colorHeaderBackground" 
 
     app:headerLayout="@layout/nav_header_main" 
 
     app:menu="@menu/activity_main_drawer" /> 
 

 
</android.support.v4.widget.DrawerLayout>

相关问题