0

listViewの項目の選択効果をカスタマイズしたいのですが、方法がわかりません。

ユーザーがアイテムを選択するときに、選択アイテムの背景を別のpngに変更する必要がある場合に役立ちます。

なにか提案を?

4

2 に答える 2

1

これにはセレクターを使用します。

<ListView android:id="@+id/list" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_gravity="center"
      android:divider="@null" 
      android:dividerHeight="0dip"
      android:listSelector="@drawable/list_selector" />

セレクター:

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

ここに良いチュートリアルがあります。

于 2012-11-29T04:52:33.353 に答える
1

あなたが探しているのはセレクターです res でセレクター xml を作成し、リスト項目の背景をセレクター xml に設定します。例:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/selected" /> <item android:drawable="@drawable/normal" /> </selector>

また見てください: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

于 2012-11-29T03:47:59.907 に答える