我尝试使用三元运算符来改变按钮的文本颜色。类似于:这里是XML。
<Button
android:id="@+id/actionButton"
android:layout_width="113dp"
android:layout_height="30dp"
android:background="@drawable/button"
android:backgroundTint="@{selected ? R.color.white : R.color.turquoise}"
android:text="@{selected ? "Selected " : "Select "}"
android:textColor="@{selected ? @color/white : @color/turquoise}"
android:onClick="@{(view) -> handler.selectClick(view)}"/>
但是颜色设置不正确,我得到了一些wierd紫色代替。
我试过了
<import type="com.myapp.R" />
android:textColor="@{selected ? R.color.white : R.color.turquoise}"
具有相同的结果。
我该怎么做呢?
2条答案
按热度按时间8dtrkrch1#
你的第一个变体应该可以正常工作。你可以参考本文档的“参考资料”一章。这里是一个完整的工作示例。
颜色.xml
主要活动.xml
活动主类
wixjitnu2#
嗨,也许我来晚了,但这里有一个设置颜色的方法:
基本上我使用上下文来获取颜色资源。希望能有所帮助。