Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析“0”的值;?android:attr/selectableItemBackground“;编码_Android_Android Custom View - Fatal编程技术网

解析“0”的值;?android:attr/selectableItemBackground“;编码

解析“0”的值;?android:attr/selectableItemBackground“;编码,android,android-custom-view,Android,Android Custom View,帮了一点忙,但没有完全解决我的问题 以下是我的XML的外观: <android.support.v7.widget.CardView ... other attributes... android:foreground="?android:attr/selectableItemBackground" > 我想,我可以使用链接的答案直接解析R.drawable.item\u background\u material,但使用?android:attr/…的关键

帮了一点忙,但没有完全解决我的问题

以下是我的XML的外观:

<android.support.v7.widget.CardView
    ... other attributes...
    android:foreground="?android:attr/selectableItemBackground"
    >
我想,我可以使用链接的答案直接解析
R.drawable.item\u background\u material
,但使用
?android:attr/…
的关键是我不知道attr指向哪里。如何利用
TypedValue
对象中包含的信息解析可绘制引用

顺便说一句,我已经试过了

val drawableRes = string.substring(string.lastIndexOf("/") + 1, string.indexOf(".xml")) // gross, but it's just a proof of concept
val drawable = resources.getIdentifier(drawableRes, "drawable", context.packageName)

但是结果总是
0

,资源标识符将位于。也就是说,在您的示例中,
typedValue.resourceId

对于任何使用Kotlin的人来说,这里有一个非常简洁的方法来实现这一点:

foreground = with(TypedValue()) {
    context.theme.resolveAttribute(R.attr.selectableItemBackground, this, true)
    ContextCompat.getDrawable(context, resourceId)
}

看这张照片。也就是说,
typedValue.resourceId
.a-ha!这完全奏效了
val drawable=context.getDrawable(typedValue.resourceId)
。如果你把这个作为答复,我会接受的。
foreground = with(TypedValue()) {
    context.theme.resolveAttribute(R.attr.selectableItemBackground, this, true)
    ContextCompat.getDrawable(context, resourceId)
}