これを行う簡単な方法は、いくつかのカスタム スタイルを作成することです。drawable フォルダーでは、通常の状態とホバーまたは押された状態のスタイルを作成できます。
したがって../drawable/
、いくつかの要素を作成する必要があります。
1)list_bg.xml
ファイル:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#db0000"
android:centerColor="#c50300"
android:endColor="#b30500"
android:angle="270" />
</shape>
2)list_bg_hover.xml
ファイル:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#db0000"
android:centerColor="#c50300"
android:endColor="#b30500"
android:angle="270" />
</shape>
3)list_selector.xml
ファイル:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_bg" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/list_bg_hover" android:state_pressed="true"/>
<item android:drawable="@drawable/list_bg_hover" android:state_pressed="false" android:state_selected="true"/>
</selector>
ListView
これを使用するには、このように行アイテムのレイアウトにスタイルをアタッチするだけですandroid:listSelector="@drawable/list_selector"
。