2014-04-04 121 views
-1

我创建了一个简单的android滑动菜单,问题是当我点击它时项目颜色不会改变。项目颜色不改变点击

这里是我的XML资源:

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/menubg" 
    android:endColor="@color/menubg" 
    android:angle="90" /> 
</shape> 

列表selector.xml:(在Draw下能)

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

activity_main.xml中:(下布局)

<com.example.app.MainLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/drawer_layout" 

> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/menu_listview" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="@color/menubg" 
     android:cacheColorHint="#00000000" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:listSelector="@drawable/list_selector" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp" 

     > 
    </ListView> 
</LinearLayout> 

回答

1

更改您的选择是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item 
    android:state_selected="false" 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:drawable="@drawable/list_item_bg_normal" /> 

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

    <item 
    android:state_selected="true" 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:drawable="@drawable/list_item_bg_pressed" /> 
</selector> 

恢复:你有几个州并与他们玩耍,你可以得到理想的效果。

+0

谢谢你:)工作。 – wissem46