【发布时间】:2015-12-29 16:47:58
【问题描述】:
Android 中的数据绑定目前似乎支持以下参考资源(根据data binding guide):@array、@color、@int、@dimen、@string... 这将给出参考值作为静态 @BindingAdapter 方法中的参数。
例如:
layout/web_view.xml
<WebView
app:htmlTextColor="@{@color/colorText}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Bindings.java
@BindingAdapter({"bind:htmlTextColor"})
public static void setHtml(WebView webView, int textColor) {
// binding logic
}
但是对于themes and styles,我更经常使用属性资源,例如?android:attr/textColorPrimary 比 @color 引用。对于这种情况,绑定"@{}" 语法会是什么样子?目前这就是我的工作方式,但也许有更好的方法?
layout/web_view.xml
<WebView
app:htmlTextColor="@{android.R.attr.textColorPrimary}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Bindings.java
@BindingAdapter({"bind:htmlTextColor"})
public static void setHtml(WebView webView, int textColorAttr) {
// binding logic
}
【问题讨论】:
标签: android android-layout android-databinding