[英]Data Binding with theme attributes
I am trying out the new Android Databinding Library and I wanted to set the background color of ToolBar using a binding.我正在试用新的 Android数据绑定库,并且想使用绑定设置 ToolBar 的背景颜色。 By default the color should be colorPrimary (from the theme).默认情况下,颜色应该是 colorPrimary(来自主题)。
Before I was using DataBinding, my toolBar looked like在我使用 DataBinding 之前,我的工具栏看起来像
<android.support.v7.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
After adding a binding, I wanted to set its background to colorPrimary if no color is bound - I'm using ternary operator for this (as mentioned in the guide) - but it causes an error, as theme attributes also have a "?"添加绑定后,如果没有绑定颜色,我想将其背景设置为 colorPrimary - 我为此使用了三元运算符(如指南中所述) - 但它会导致错误,因为主题属性也有一个“?” operator before their names.操作员在他们的名字之前。 The compiler thinks I'm starting a new ternary operation.编译器认为我正在开始一个新的三元运算。
<data>
<variable name="toolBarBackgroundColor" type="int"/>
</data>
...
<android.support.v7.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@{toolBarBackgroundColor!=0? toolBarBackgroundColor: ?attr/colorPrimary }"
/>
So is there a way I can access theme attributes inside a binding operation?那么有没有办法可以在绑定操作中访问主题属性? Thanks!谢谢!
Edit编辑
I know I can get the colorPrimary attribute programmatically and bind it through java code.我知道我可以通过编程方式获取 colorPrimary 属性并通过 java 代码绑定它。 But I'm just wondering if there's an Xml-based solution for this or not.但我只是想知道是否有基于 Xml 的解决方案。
The answer is a bit late, but maybe it helps someone.答案有点晚了,但也许它可以帮助某人。
For accessing theme attributes in data binding, you can use this:要访问数据绑定中的主题属性,您可以使用:
(imagine that clickable
is Boolean
variable) (想象一下clickable
是Boolean
变量)
android:background="@{clickable ? android.R.attr.selectableItemBackground : android.R.color.transparent}"
No additional binding adapters or another things needed.不需要额外的绑定适配器或其他东西。
Finding a way using data-binding?寻找使用数据绑定的方法? Here is what I have done with test.这是我对测试所做的。 First, create a custom binding adapter method:首先,创建一个自定义绑定适配器方法:
@BindingAdapter({"app:customPrimaryBackground"})
public static void setCustomPrimaryBackground(View v, int resId) {
TypedValue typedValue = new TypedValue();
Context context = v.getContext();
if (resId == 0) {
context.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
v.setBackgroundResource(typedValue.resourceId);
} else {
// Check the given resource ID is a color or drawable
context.getResources().getValue(resId, typedValue, true);
Drawable drawable;
if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// It's a color
drawable = new ColorDrawable(typedValue.data);
} else {
drawable = ContextCompat.getDrawable(context, resId);
}
v.setBackground(drawable);
}
}
Second, your binding xml layout:其次,您的绑定 xml 布局:
<data>
<variable name="toolBarBackgroundColor" type="int"/>
</data>
...
<android.support.v7.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:customPrimaryBackground="@{toolBarBackgroundColor}"
/>
如果问题对某人来说仍然是实际的,这是如何使用自定义和 android 属性app:textColorAttr="@{error ? com.example.R.attr.textColorError : android.R.attr.textColor}"
,其中textColorAttr 使用BindingAdapter
实现, error
是Boolean
, com.example
是你的包名
I found another solution without custom attributes and binding adapters.我找到了另一个没有自定义属性和绑定适配器的解决方案。 The idea is to place an invisible View
with android:background
and android:foreground
attributes in XML markup and use these attributes in a binding expression.这个想法是在 XML 标记中放置一个具有android:background
和android:foreground
属性的不可见View
,并在绑定表达式中使用这些属性。
<data>
<variable name="toolBarBackgroundColor" type="int"/>
</data>
...
<android.support.v7.widget.Toolbar
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@{toolBarBackgroundColor != 0 ? helperView.getBackground() : helperView.getForeground() }"
/>
<View
android:id="@+id/helperView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@{toolBarBackgroundColor}"
android:foreground="?attr/colorPrimary"
android:visibility="gone"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.