[英]selectableItemBackground not working for Android 4.4.2 Device
I am trying to set the onClick()
highlight for each row in my TableLayout
. 我正在尝试为
TableLayout
每一行设置onClick()
高亮显示。
I have a drawable resource file: 我有一个可绘制的资源文件:
<item
android:drawable="?android:attr/selectableItemBackground"
android:state_focused="true"
android:state_pressed="true" />
<!-- Bottom border -->
<item android:top="65dp" android:left="15dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:height="0.25dp"/>
<solid android:color="@color/dark_blue"/>
</shape>
</item>
I have tested this on a Samsung Galaxy S5 device which works. 我已经在可以正常工作的Samsung Galaxy S5设备上对此进行了测试。 However, when I test on a ZTE Compel Device, which has Android 4.4.2 installed on it -- it does not work.
但是,当我在安装了Android 4.4.2的ZTE Compel设备上进行测试时,它不起作用。
My application is targeting API 15 and up. 我的应用程序的目标是API 15及更高版本。
Any ideas why this: ?android:attr/selectableItemBackground
doesn't work on Android 4.4.2 device? 任何想法为什么这样:
?android:attr/selectableItemBackground
在Android 4.4.2设备上不起作用?
Error in logs: 日志错误:
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: tag requires a 'drawable' attribute or child tag defining a drawable
引起原因:org.xmlpull.v1.XmlPullParserException:二进制XML文件第7行:标记需要一个'drawable'属性或定义一个drawable的子标记
I have ?attr/selectableItemBackground
and it doesn't resolve the issue. 我有
?attr/selectableItemBackground
,它不能解决问题。 I have done a ton of research. 我做了很多研究。
Any other suggestions from your side? 您还有其他建议吗?
The issue was in my drawable Resource File I was using a layer-list. 问题出在我使用图层列表的可绘制资源文件中。 Drawable attribute in a layer-list (or state-list) does not accept an attr value.
图层列表(或状态列表)中的Drawable属性不接受attr值。
However, I couldn't set this attr value in my layout file because I also needed a bottom border for each table row. 但是,我无法在布局文件中设置此attr值,因为我还需要为每个表行添加底部边框。
So to fix this, in the Java Class file: 因此,要解决此问题,请在Java Class文件中:
I retrieve a reference to each row in my onCreate() method. 我在onCreate()方法中检索对每一行的引用。
Example: TableRow row1 = (TableRow)findViewById(R.id.table_row_1);
示例:
TableRow row1 = (TableRow)findViewById(R.id.table_row_1);
Then I created a method that would handle adding the attribute to the bottom_border drawable file: 然后,我创建了一个方法,用于处理将属性添加到bottom_border可绘制文件中:
`void setRowHighlight(TableRow row) {
// Attribute array
int[] attrs = new int[] { android.R.attr.selectableItemBackground };
TypedArray a = getTheme().obtainStyledAttributes(attrs);
// Drawable held by attribute 'selectableItemBackground' is at index '0'
Drawable d = a.getDrawable(0);
a.recycle();
if (Build.VERSION.SDK_INT < 16) {
LayerDrawable ld = new LayerDrawable(new Drawable[] {
// Table Rows Border Drawable
getResources().getDrawable(R.drawable.table_rows_border),
// Drawable from attribute
d });
// Set the background to 'ld'
row.setBackgroundDrawable(ld);
} else {
LayerDrawable ld = new LayerDrawable(new Drawable[] {
// Table Rows Border Drawable
ResourcesCompat.getDrawable(getResources(), R.drawable.table_rows_border, null),
// Drawable from attribute
d });
// Set the background to 'ld'
row.setBackground(ld);
}
}`
Enable the row highlight, call my method in the onCreate(): setRowHighlight(row1);
启用行突出显示,在onCreate()中调用我的方法:
setRowHighlight(row1);
I have tested this on my Samsung Galaxy S5 device (Android 5.0) and ZTE Compel device (Android 4.4.2) and it works. 我已经在我的Samsung Galaxy S5设备(Android 5.0)和ZTE Compel设备(Android 4.4.2)上进行了测试,并且可以正常工作。
Reference: ?android:attr/selectableItemBackground with another existing background 参考: ?android:attr / selectableItemBackground与另一个现有背景
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.