项目颜色在点击时不会改变

时间:2014-04-04 16:08:27

标签: android xml

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

这是我的xml资源:

list_item_bg_pressed.xml:(在Drawable下)

<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:(在Drawable下)

<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:(在Drawable下)

<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 个答案:

答案 0 :(得分: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>

恢复:你将拥有几种状态,并且可以使用它们,你可以获得所需的效果。