TextView textColor中的数据绑定选择器 [英] Databinding selector in TextView textColor
本文介绍了TextView textColor中的数据绑定选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据频道中未读消息的数量从textview设置颜色。像这样:
I am trying to set colors from a textview based on the number of unread messages in a channel. Like so:
android:textColor="@{channel.unreadCount > 0 ? @color/selector_conversation_row_title_unread : @color/selector_conversation_row_title_read}"
这仅设置标题的颜色,而:
this only sets the color of the title, while:
android:textColor="@color/selector_conversation_row_title_unread"
此代码将textColor设置为选择器,如果我按TextView,则颜色变化与第一条语句不同。
this code sets the textColor as a selector, and if i press the TextView the color changes unlike the first statement.
selector_conversation_row_title_unread:
selector_conversation_row_title_unread:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/colorConversationTitleUnread"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#ffffff"/>
<item android:state_focused="false" android:state_pressed="true" android:color="#ffffff"/>
<item android:color="@color/colorConversationTitleUnread"/>
</selector>
selector_conversation_row_title_read:
selector_conversation_row_title_read:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/colorConversationTitle"/>
<item android:state_focused="true" android:state_pressed="true" android:color="#ffffff"/>
<item android:state_focused="false" android:state_pressed="true" android:color="#ffffff"/>
<item android:color="@color/colorConversationTitle"/>
</selector>
为什么选择器仅用作:
android:textColor="@color/selector_conversation_row_title_unread"
推荐答案
Android数据绑定不知道资源类型,因此您必须在表达式中提供它:
Android Data Binding doesn't know about resource types, so you must supply it in the expression:
android:textColor="@{channel.unreadCount > 0 ? @colorStateList/selector_conversation_row_title_unread : @colorStateList/selector_conversation_row_title_read}"
这篇关于TextView textColor中的数据绑定选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文