2

我試着尋找通過幾個SO回答這個問題,但他們都沒有爲我工作。不能風格選定抽屜項目的背景顏色

我正在嘗試更改我的導航抽屜中選定項目的背景顏色(the gray)。

這裏是我的代碼:

menu_drawer

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
     <item 
      android:icon="@drawable/ic_one" 
      android:id="@+id/nav_first_fragment" 
      android:title="One" 
      android:checked="true"/> 
     <item 
      android:icon="@drawable/ic_two" 
      android:id="@+id/nav_second_fragment" 
      android:title="Two" /> 
     <item 
      android:icon="@drawable/ic_three" 
      android:id="@+id/nav_third_fragment" 
      android:title="Three" /> 
    </group> 
</menu> 

activity_main

<?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" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:elevation="0dp"> 

      <include 
       layout="@layout/toolbar" 
       /> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      /> 

    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nvView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@android:color/white" 
     app:menu="@menu/menu_drawer" /> 

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

請幫幫忙!

回答

1

檢查與此屬性

itemBackground

在你的顏色目錄

然後有一個XML文件,這樣

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:color="#c0c0c0" android:state_checked="true" /> 
<item android:color="#ffffff" /> 
</selector> 

東西然後添加到NavigationView

應用:itemBackground = 「@顏色/ nv_list」

更新:

入住這doc

+0

在哪裏可以找到導航抽屜使用的各種狀態? – user5382818

+0

@ user5382818,檢查文檔鏈接 –

0

把以下文件夾繪製:

list_selectoritem.xml

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

    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/> 
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/> 
    <item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/> 

</selector> 

list_item_bg_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="@color/list_background_pressed" 
     android:endColor="@color/list_background_pressed" 
     android:angle="90" /> 
</shape> 

list_item_bg_normal.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="@color/list_background" 
     android:endColor="@color/list_background" 
     android:angle="90" /> 
</shape> 

在color.xml

<color name="list_background">#E2231A</color> 
<color name="list_background_pressed">#C4BEB6</color> 
<color name="list_divider">#ffffff</color> 

然後將更改應用到你的抽屜的列表視圖下面將下面的代碼;

<ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="300dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@null" 
     android:dividerHeight="1dp"  
     android:listSelector="@drawable/list_selectoritem" 
     android:background="@color/list_background"/>