rek*_*ire 5 android android-attributes android-databinding android-binding-adapter
我有一个来自自定义视图定义行的自定义属性:
<declare-styleable name="ExampleView">
<attr name="order">
<enum name="byValue" value="0" />
<enum name="byKey" value="1" />
</attr>
<!-- and some more attributes -->
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
Android Studio 检测到这一点并为我提供自动完成功能,这很棒。所以 xml 属性看起来像app:order="byValue". 但是,由于我想使用BindingAdapter数据绑定 API 中的 a,因此我需要将它与这样的@符号一起使用:app:order="@{byValue}",不幸的是这不能编译。
然后我尝试使用我在内部使用的常量,如下所示:app:order="@{com.example.views.ExampleView.ORDER_BY_VALUE}",但这也不能编译。我可以只使用app:order="@{0}",确保这有效,因为它是这样定义的,但是为什么我在0那里使用它并不直观。
知道如何编写更具可读性的代码来解决此问题吗?
有必要为枚举值创建代码:
object Order {
const val BY_VALUE = 0
const val BY_KEY = 1
}
Run Code Online (Sandbox Code Playgroud)
将保存这些枚举的类/对象导入到您的 XML 中:
<import type="com.example.Order" />
Run Code Online (Sandbox Code Playgroud)
然后你可以引用它们:
app:order="@{Order.INSTANCE.BY_KEY}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |