Com*_*are 11 android android-resources android-databinding
Florina Muntenescu撰写了一篇关于<annotation>在字符串资源中使用能够拥有灵活标记的酷文章,您可以使用自定义跨度在应用程序中处理这些标记.我试图在数据绑定中利用它,但我无法弄清楚如何SpannedString从数据绑定中获取字符串资源的版本.
在我的布局中,我有app:thingy="@{@string/my_annotated_string}"一个属性TextView.我有一个绑定适配器设置来处理thingy属性.但是,数据绑定系统似乎坚持我的价值是String.
我试过了:
@BindingAdapter("thingy")
@JvmStatic
fun handleThingy(textView: TextView, thingy: SpannedString) { /* stuff goes here */ }
Run Code Online (Sandbox Code Playgroud)
和:
@BindingAdapter("thingy")
@JvmStatic
fun handleThingy(textView: TextView, thingy: Spanned) { /* stuff goes here */ }
Run Code Online (Sandbox Code Playgroud)
和:
@BindingAdapter("thingy")
@JvmStatic
fun handleThingy(textView: TextView, @StringRes thingy: Int) { /* stuff goes here */ }
Run Code Online (Sandbox Code Playgroud)
在所有情况下,我都会遇到Cannot find the setter for attribute 'app:thingy' with parameter type java.lang.String on android.widget.TextView构建错误.
如果我使用String或CharSequence为thingy参数类型,它构建,但然后我传递了一个String,我没有我的注释跨越字符串资源.
那么,我怎么能:
SpannedString与我的字符串资源相对应的内容(即,您从中获取的内容getText()而不是getString()),或者getText()自己来获取我的SpannedString作为表达式,@string/my_annotated_string计算结果为字符串。尽管它类似于 XML 中的字符串资源引用,但它实际上只是一个String值。
最好有一个 @text/my_annotated_string版本会很好,但是在文档中这是不可用的。
相反,您必须在绑定表达式中使用实际资源:
app:thingy="@{string.my_annotated_string}"
app:thingy="@{context.getText(string.my_annotated_string)}"
Run Code Online (Sandbox Code Playgroud)
这是假设string类的导入:
<import type="path.to.R.string"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
367 次 |
| 最近记录: |