如果你想用股票上點擊亮點像你這樣一個通用的ListView得到,你要設置的每個行的背景是
android:background="@drawable/selector"
下面是一個例子:
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="5dip"
android:background="@drawable/selector">
這是selector.xml
在res\drawable
文件夾
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/blue></item>
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/custom"></item>
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@color/gray" />
<item android:drawable="@color/white"></item>
</selector>
更新:創建StateListDrawable
編程像下面並設置爲Background
您TableRow
:
Drawable d1=activity.getResources().getDrawable(R.drawable.gradient_bg_hover);
GradientDrawable g = new GradientDrawable(Orientation.TOP_BOTTOM,
new int[] { Color.DKGRAY});
g.setGradientType(GradientDrawable.LINEAR_GRADIENT);
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed,-android.R.attr.state_selected},d1);
states.addState(new int[] {-android.R.attr.state_focused},g);
table_row.setBackgroundDrawable(states);
這是res\drawable
文件夾gradient_bg_hover.xml
。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient BgColor for listrow Selected -->
<gradient
android:startColor="#18d7e5"
android:centerColor="#16cedb"
android:endColor="#09adb9"
android:angle="270" />
</shape>
UPDATE2:您可以添加更多State
到StateListDrawable
按您的要求。
android:state_activated:
時設置視圖或其父已被「激活」,這意味着用戶目前已經將其標記爲感興趣。
android:state_active:
StateListDrawable
的狀態值。
android:state_enabled:
啓用視圖時設置。
android:state_focused:
當視圖具有輸入焦點時設置的StateListDrawable
的狀態值。
android:state_pressed:
當用戶在視圖中按下時設置。
android:state_selected:
噹噹前選擇視圖(或其父項之一)時設置。
更多有關StateListDrawable
看到我的新的更新ü只是需要更多的'State'加入到'StateListDrawable'爲每U [R要求 –