Android的设置TextView的文字颜色编程方式与setTextColor()使机器人:duplicateParentState无效 [英] Setting Android TextView text color programmatically with setTextColor() renders android:duplicateParentState invalid

查看:1211
本文介绍了Android的设置TextView的文字颜色编程方式与setTextColor()使机器人:duplicateParentState无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的文字项的列表视图。这些项目中的TextView看起来是这样的(它的包装在一个 RelativeLayout的):

I have a listview of simple text items. The TextView in those items looks like this (it's wrapped inside a RelativeLayout):

<TextView
  android:id="@+id/text_language_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_centerVertical="true"
  android:duplicateParentState="true"
  android:textSize="16sp"
  android:textStyle="bold" />

我想用下面的颜色状态选择文本,这是我所谓的dark_list_text_states.xml:

I want to use the following color state selector for the text, which I have called "dark_list_text_states.xml":

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/white"/>
    <item android:color="@color/dark_list_text"/>

</selector>

通常情况下,当然,我可以只设置为安卓文字颜色的xml属性,但在这种情况下,我需要通过编程设定一个特定的项目不同的颜色,使用该选择器(blue_text_states.xml):

Normally, of course, I could just set that to the android:textColor property in the xml, but in this case, I need to set one particular item programmatically to a different color, using this selector ("blue_text_states.xml"):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@android:color/white"/>
    <item android:color="@color/selected_blue"/>

</selector>

所以我在适配器设置文本颜色( LANGUAGE_NAME 选择出现在靠前$ C $定义C):

So I set the text color in the Adapter (language_name and selected are defined earlier in the code):

        TextView text_language_name = (TextView)view.findViewById(R.id.text_language_name);
        if (text_language_name != null) {
            text_language_name.setText(language_name);
            int text_color = selected
                           ? getResources().getColor(R.color.blue_text_states)
                           : getResources().getColor(R.color.dark_list_text_states);
            text_language_name.setTextColor(text_color);
            text_language_name.setDuplicateParentStateEnabled(true);
        }

和这工作正常,但有一个问题:该项目被点击时文本颜色没有变化。即使我叫 setDuplicateParentStateEnabled(真)该项目之后 setTextColor(),该设置将完全忽略。

And this works fine, except for one problem: the text color doesn't change when the item is clicked. Even though I call setDuplicateParentStateEnabled(true) on the item right after setTextColor(), that setting is completely ignored.

我已经测试了这个被注释掉了Java code,设置文本颜色,只是在XML设置它,并且选择工作得很好。但是,如果我以编程方式设置文字颜色,它是完全无法复制的父状态。

I have tested this by commenting out the Java code that sets the text color and just setting it in the xml, and the selector works just fine. But if I set textColor programmatically, it is completely incapable of duplicating the parent state.

有没有人见过这个?有一种解决方法,或者这只是一个Android的bug我不得不忍受?

Has anyone seen this before? Is there a workaround, or is this just an Android bug I have to live with?

推荐答案

OK,发现我的错误。我应该使用 getResources()。getColorStateList()而不是 getResources()的getColor()。离开这个在这里谁比谁犯同样的错误。

OK, found my mistake. I should be using getResources().getColorStateList() instead of getResources().getColor(). Leaving this here for anyone else who makes the same mistake.

这篇关于Android的设置TextView的文字颜色编程方式与setTextColor()使机器人:duplicateParentState无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆