2016-04-28 129 views
0

我想要有一個導航視圖,其中第一組textColor是黑色,第二組是lightGrey。Android NavigationView雙文本顏色

我似乎只能在NavigationView xml配置中指定一種顏色。

有沒有辦法來覆蓋這種行爲?

我的導航視圖菜單:

<group 
    android:id="@+id/navGroupMain" 
    android:checkableBehavior="single"> 
    <item 
     android:id="@+id/nav_dashboard" 
     android:icon="@drawable/ic_dashboard" 
     android:title="@string/navigation_drawer_item_dashboard" /> 
</group> 

<group 
    android:id="@+id/navGroupFooter" 
    android:checkableBehavior="single"> 
    <item 
     android:id="@+id/nav_settings" 
     android:icon="@drawable/ic_settings" 
     android:title="@string/navigation_drawer_item_settings" /> 
</group> 

這是插入NavigationView

<style name="AppTheme.ThemeOverlay.NavigationView" parent="ThemeOverlay.AppCompat.Light"> 
    <!-- This is the menu item text color. --> 
    <item name="android:textColorPrimary">@color/black</item> 
    <!-- This is the menu header text color and icon color. --> 
    <item name="android:textColorSecondary">@color/grey_500</item> 
    <!-- This is the selected color. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
</style> 
+0

文章最後一段代碼,請 – Haroon

+0

我的代碼工作,請接受它的答案! –

回答

0

好這部作品主題,但它是非常髒。 i [0] < 16表示它應該只更改前4項的背景。

將它添加到您的onCreate方法

LayoutInflater layoutInflater = getLayoutInflater(); 
     try { 
      Field field = LayoutInflater.class.getDeclaredField("mFactorySet"); 
      field.setAccessible(true); 
      field.setBoolean(layoutInflater, false); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (NoSuchFieldException e) { 
      e.printStackTrace(); 
     } 
     final int[] i = {0}; 
     LayoutInflaterCompat.setFactory(getLayoutInflater(), new LayoutInflaterFactory() { 
      @Override 
      public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 
       i[0]++; 
       if (i[0] < 16 && name .equalsIgnoreCase("android.support.design.internal.NavigationMenuItemView")) { 
        try{ 
         LayoutInflater f = getLayoutInflater(); 
         final View view = f.createView(name, null, attrs); 
         new Handler().post(new Runnable() { 
          public void run() { 
           // set the background drawable 
           view .setBackgroundColor(Color.CYAN); 

          } 
         }); 
         return view; 
        } catch (InflateException e) { 
        } catch (ClassNotFoundException e) {} 
       } 
       return null; 
      } 
     });