我有一个表行包含多个数据。单击某行时,该特定行将用深灰色突出显示。但是,当我单击下一行时,单击的行将突出显示,但上一行仍将突出显示。如何禁用上一行的突出显示。我尝试了许多技巧,但这没有用。
这是我的示例代码。
tableRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundColor(Color.DKGRAY);
}
我已经以编程方式创建了布局。我没有使用XML。
提前致谢。
如果要像使用普通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);
这gradient_bg_hover.xml
在res\drawable
文件夹中。
<?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的更多信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句