Android 单击行时无法突出显示动态创建的TableLayout行的背景
我以编程方式动态创建了一个TableLayout。但我无法在单击任何行时突出显示该行Android 单击行时无法突出显示动态创建的TableLayout行的背景,android,Android,我以编程方式动态创建了一个TableLayout。但我无法在单击任何行时突出显示该行 非常感谢您提供的任何帮助。请尝试此选项,并为单击表格行进行自定义: protected void onListItemClick(ListView parent, View view, int position, long id) { super.onListItemClick(parent, view, position, id); LinearLayout ActiveIt
非常感谢您提供的任何帮助。请尝试此选项,并为单击表格行进行自定义:
protected void onListItemClick(ListView parent, View view, int position, long id) {
super.onListItemClick(parent, view, position, id);
LinearLayout ActiveItem = (LinearLayout) view;
ActiveItem.setBackgroundResource(R.drawable.background_dark_blue);
}
background_dark_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#ffffffff" />
<!-- border width and color -->
<stroke
android:width="2dp"
android:color="#FF0404B4" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
</shape></item>
</layer-list>
您好,谢谢您的帮助,上面的工作很好,但这不是我想要实现的。我只想高亮显示(更改行的背景色,类似于单击按钮)该行,直到用户按下该行为止,一旦释放,该行应保留其原有颜色。您好,我尝试了您编辑的解决方案,但我遇到以下异常:11-27 15:46:43.754:ERROR/AndroidRuntime(499):android.content.res.Resources$NotFoundException:文件res/drawable hdpi/color_selector.xml from drawable resource ID#0x7f020001 11-27 15:46:43.754:ERROR/AndroidRuntime(499):原因:org.xmlpull.v1.XmlPullParserException:二进制XML文件行#3:标记需要定义可绘制文件的“drawable”属性或子标记,尽管我已将资源文件正确放置在指定位置。您应该在“res\drawable”文件夹中定义state_orange.XML和background_dark_blue.XML。它们在res\drawable文件夹中folderrefreshed也重新启动了eclipse。问题仍然存在:(
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused states -->
<item android:drawable="@drawable/state_orange" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/state_orange" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
<!-- Pressed -->
<item android:drawable="@drawable/state_orange" android:state_pressed="true"/>
<!-- Non focused states -->
<item android:drawable="@drawable/background_dark_blue" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/state_orange" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
</selector>
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/orange" />
</shape>
android:background="@color/back_color_selector_blue_gradient"