我正在尝试新的 Android数据绑定库 https://developer.android.com/tools/data-binding/guide.html我想使用绑定设置工具栏的背景颜色。默认情况下,颜色应为 colorPrimary(来自主题)。
在使用 DataBinding 之前,我的工具栏看起来像
<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 }"
/>
那么有没有办法可以在绑定操作中访问主题属性?谢谢!
Edit
我知道我可以通过编程方式获取 colorPrimary 属性并通过 java 代码绑定它。但我只是想知道是否有基于 Xml 的解决方案。
答案有点晚了,但也许对某人有帮助。
要访问数据绑定中的主题属性,您可以使用以下命令:
(想象一下clickable
is Boolean
多变的)
android:background="@{clickable ? android.R.attr.selectableItemBackground : android.R.color.transparent}"
不需要额外的绑定适配器或其他东西。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)