2

我用NavigationDrawer和COMMUN材料設計menu.xml這樣的設計佈局,但我不能找到一種方法,使不同顏色的菜單項的每一行。怎麼做?如何更改NavigationDrawer材質設計中每個菜單項的背景顏色?

這裏是我的抽屜式導航欄中的xml:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/transparent" 
    android:id="@+id/drawer_layout" 
    xmlns:app="http://schemas.android.com/apk/res-auto" > 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:itemTextColor="#333" 
     app:itemIconTint="#333" 
     app:headerLayout="@layout/drawer_header" 
     app:menu="@menu/drawer" /> 
</android.support.v4.widget.DrawerLayout> 
+0

你可以只發布代碼,你試過了什麼? –

+0

*請參閱http://www.tutecentral.com/android-custom-navigation-drawer/ – karthik

+0

以及我已經嘗試programetically創建navigationmenu和並儘量選擇菜單項,但沒有發現設置背景顏色#菜單項的任何財產Nigam Patro –

回答

0

AFAIK它不是簡單的做你是什麼高達。如果您不想爲導航抽屜進行這種類型的自定義,那麼如果您在NavigationView中提供整個視圖,然後可以以任何您想要的方式對其進行設置,那將會更好。

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

     <LinearLayout 
      android:id="@+id/linear_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="@color/white" 
      android:orientation="vertical" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

      ... 

     </LinearLayout> 
</android.support.design.widget.NavigationView> 

現在,您可以按照需要的方式在此線性佈局內創建菜單視圖。我希望這可以幫助你..

+0

哦是的,它是自定義菜單,我看到之後很長一段時間對不起的最佳方式進行編程好主意我忘了選擇你的答案,yaa它對我自定義我很有幫助謝謝你,@yash Ladia。 –

0

使用編譯 'com.android.support:appcompat-v7:23.1.1'

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:headerLayout="@layout/nav_header_main_navigation" 
    app:itemBackground="@drawable/nav_view_item_background" 
    app:itemTextColor="@color/nav_item_text_color" 
    app:menu="@menu/activity_main_navigation_drawer" /> 

用來對項目研究背景下面的XML文件:nav_view_item_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@color/colorPrimary" android:state_checked="true" /> 
<item android:drawable="@android:color/transparent" /> 

用來爲文本顏色下面的XML文件:nav_item_text_color

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:color="@color/white" android:state_checked="true" /> 
<item android:color="@android:color/black" /> 

相關問題