TextView textColor中的数据绑定选择器

时间:2016-09-13 14:27:25

标签: android data-binding textview selector textcolor

我正在尝试根据频道中未读消息的数量设置textview中的颜色。像这样:

android:textColor="@{channel.unreadCount > 0 ? @color/selector_conversation_row_title_unread : @color/selector_conversation_row_title_read}"

这只设置标题的颜色,而:

android:textColor="@color/selector_conversation_row_title_unread"

此代码将textColor设置为选择器,如果按下TextView,颜色会与第一个语句不同。

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:

<?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"

1 个答案:

答案 0 :(得分:13)

Android数据绑定不了解资源类型,因此您必须在表达式中提供它:

android:textColor="@{channel.unreadCount > 0 ? @colorStateList/selector_conversation_row_title_unread : @colorStateList/selector_conversation_row_title_read}"