【问题标题】:TextView setTextColor() not workingTextView setTextColor()不起作用
【发布时间】:2011-09-04 20:35:00
【问题描述】:

我以编程方式创建此类元素的列表(不是 ListView,只是将它们添加到父级):

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="vertical" android:layout_weight="1">
    <TextView android:id="@+id/filiale_name"
    android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/lagerstand_text"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="10sp" android:textColor="@color/red"/>
</LinearLayout>

另外,我在 values/colors.xml 中定义了一些颜色。如您所见,ID 为“lagerstand_text”的 TextView 默认将其颜色设置为红色。这行得通。

在 Java 中创建元素时,我会这样做

lagerstandText.setText("bla");

对于某些元素我也这样做

lagerstandText.setTextColor(R.color.red);

和其他颜色。虽然我不调用 setTextColor() 的元素是红色的,但所有其他元素都是灰色的,无论我选择哪种颜色(即使它再次是相同的红色)。

这是为什么呢?

【问题讨论】:

    标签: android colors textview


    【解决方案1】:

    文档对此不是很详细,但在调用setTextColor 时不能只使用 R.color 整数。您需要调用getResources().getColor(R.color.YOURCOLOR) 正确设置颜色。

    使用以下代码以编程方式设置文本颜色:

    textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
    

    从支持库 23 开始,您必须使用以下代码,因为不推荐使用 getColor:

    textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
    

    【讨论】:

    • 好的,这行得通。在这种情况下,API 文档可能会更冗长...
    • 你也可以使用颜色。 (这里有红绿黑蓝黄等) setTextColor(Color.RED)
    • 感谢您的信息...但是这个 getResources() 让我传递了一系列上下文。应该有更好的方法来访问全球资源。
    • @Umair 在您的应用程序类上使用静态变量。
    • @Kontinuity,我读到将上下文保存在静态变量中是内存泄漏的重要来源。
    【解决方案2】:

    所以,有很多方法可以完成这项任务。

    1.

    int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
    textview.setTextColor(color);
    

    2.

    textView.setTextColor(getResources().getColor(R.color.some_color));
    

    3.

    textView.setTextColor(0xffbdbdbd);
    

    4.

    textView.setTextColor(Color.parseColor("#bdbdbd"));
    

    5.

    textView.setTextColor(Color.argb(a_int, r_int, g_int, b_int));
    

    【讨论】:

    • 有什么方法可以查出一个特定的颜色值是否会使文本消失?
    • 在使用 setTextColor(color) 之前,我正在调整颜色的亮度。在某些未知情况下,TextView 会在设备上消失(独立于背景)。我想在 setTextColor(color) 中使用之前编写一个测试函数来检查“color”是否是有效的颜色值。
    • @ChristopherMasser 没有尝试你所说的任何内容??
    【解决方案3】:

    1.您喜欢的标准颜色请选择下面。

    textview.setTextColor(Color.select_color)
    

    2.这里要使用自定义颜色添加到color.xml文件中

    textview.setTextColor(getResources().getColor(R.color.textbody));
    

    textView.setTextColor(Color.parseColor("#000000"));
    

    subText.setTextColor(Color.rgb(255,192,0));
    

    【讨论】:

      【解决方案4】:

      为了将来参考,您可以使用以下内容:

      String color = getString(Integer.parseInt(String.valueOf(R.color.my_color)));
      my_textView.setTextColor(Color.parseColor(color));
      

      这样您就可以利用您的颜色资源。

      【讨论】:

        【解决方案5】:

        R 类中定义的特定颜色的整数 id(在 xml 布局中定义)不能作为参数传递给 View 类的 setTextColor() 方法。 您必须通过以下代码行获取setTextColor()的参数:

        int para=getResources().getColor(R.color.your_color,null);
        view.setTextColor(para,null);
        

        getColor(int id) 方法已被弃用...改为使用getColor(int id,Resources.Theme theme),如上面的代码行。

        The `second parameter( theme )` can be null
        

        【讨论】:

          【解决方案6】:
          textView.setTextColor(Color.RED);
          

          【讨论】:

          • 您的回答与其他成员发布的回答有何不同?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-15
          • 2023-04-05
          • 2020-09-19
          相关资源
          最近更新 更多