数据绑定与主题属性

时间:2015-07-05 22:15:58

标签: android data-binding android-theme

我正在尝试新的Android Databinding Library,我想使用绑定设置ToolBar的背景颜色。默认情况下,颜色应为colorPrimary(来自主题)。

在我使用DataBinding之前,我的toolBar看起来像

 <android.support.v7.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        />

添加一个绑定后,如果没有绑定颜色,我想将其背景设置为colorPrimary - 我正在使用三元运算符(如指南中所述) - 但它会导致错误,因为主题属性也有“?”操作员在他们的名字前编译器认为我正在开始新的三元操作。

<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 }"
        />

那么有没有办法可以在绑定操作中访问主题属性?谢谢!

修改

我知道我可以通过编程方式获取colorPrimary属性并通过java代码绑定它。但我只是想知道是否有基于Xml的解决方案。

4 个答案:

答案 0 :(得分:1)

答案有点晚,但也许对某人有帮助。

要访问数据绑定中的主题属性,您可以使用:

(假设clickableBoolean变量)

android:background="@{clickable ? android.R.attr.selectableItemBackground : android.R.color.transparent}"

不需要额外的绑定适配器或其他东西。

答案 1 :(得分:0)

找到使用数据绑定的方法?这是我在测试中所做的。首先,创建一个自定义绑定适配器方法:

@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);
    }
}

其次,你的绑定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}"
/>

答案 2 :(得分:0)

如果该问题对于某人仍然是实际的,则此示例为如何使用自定义和android属性的示例 app:textColorAttr="@{error ? com.example.R.attr.textColorError : android.R.attr.textColor}",其中使用BindingAdapter实现的textColorAttr,errorBoolean,而com.example是您的包名称

答案 3 :(得分:0)

我找到了另一个没有自定义属性和绑定适配器的解决方案。这个想法是在 XML 标记中放置一个具有 Viewandroid:background 属性的不可见 android:foreground,并在绑定表达式中使用这些属性。

<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"
    />
相关问题