如果你想用股票上点击亮点像你这样一个通用的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要求 –