如何在指纹和手指释放时更改列表视图背景渐变

时间:2013-01-17 13:39:18

标签: java android listview user-interface gradient

如何在点击时将背景渐变更改为某些内容并在手指释放时获取原始渐变...我使用列表视图适配器创建的列表确实更改为渐变但在我抬起时不会更改回原始渐变手指.. xmls下面

main.xml中

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#EDEDED"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
  />

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->

<item android:state_focused="true"
    android:drawable="@drawable/gradient_bg_hover"
    />

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

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

   <item android:state_selected="true"
    android:state_pressed="false"
    android:drawable="@drawable/gradient_bg_hover" />

</selector>

row_list.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txtListText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="15dp"
    android:textSize="45sp"


    />

gradient_bg和gradient_bghover是渐变xml的..

1 个答案:

答案 0 :(得分:0)

我想,你不能一次定义state_selected和state_pressed ......

工作的东西应该是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/_bt_week_selected" />
    <item android:state_focused="true" android:drawable="@drawable/_bt_week_selected"/>
    <item android:state_pressed="true" android:drawable="@drawable/_bt_week_selected"/>
    <item android:drawable="@drawable/_bt_week_normal"/>
</selector>

并将列表项(row_list)的背景设​​置为选择器...例如android:background="@drawable/selector" ...而不是使用android:listSelector="@drawable/list_selector" 我不知道你的工作是否正常......