Databinding selector in TextView textColor

南楼画角 提交于 2020-02-27 07:56:48

问题


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"

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:

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

Why does the selector only work as?:

android:textColor="@color/selector_conversation_row_title_unread"

回答1:


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


来源:https://stackoverflow.com/questions/39472503/databinding-selector-in-textview-textcolor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!