解析“0”的值;?android:attr/selectableItemBackground“;编码
帮了一点忙,但没有完全解决我的问题 以下是我的XML的外观:解析“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/…的关键
<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)
}