安卓TextView:动态设置背景颜色无效

181

在 Android 中编程设置 TextView 的背景颜色似乎不起作用。我是否漏掉了什么?

TextView et = new TextView(activity);
et.setText("350");
et.setBackgroundColor(R.color.white);

我也在我的 res/values 文件夹中有这个文件 (colors.xml)

<resources>
        <color name="white">#ffffffff</color>
        <color name="black">#ff000000</color>
</resources>

[编辑]:同时,设置文本颜色会导致 TextView 消失。

TextView c1 = new TextView(activity);
c1.setTextColor(R.color.solid_red);
c1.setText("My Text");
16个回答

3

Color.parseHexColor("17ee27") 对我来说不起作用,而Color.parseColor("17ee27")完美运行。


2

如果您想支持所有版本: 请尝试这样做:

myTextView.setBackgroundColor(ContextCompat.getColor(this,R.color.mycolor)); 

1
只需使用


ArrayAdapter<String> adaptername = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, your array list);

0

如果要从资源中设置颜色,请按照以下步骤:

textView.setBackgroundColor(getResources().getColor(R.color.ButtonColorRed));

这里的ButtonColorRed是颜色资源中的颜色名称


0
如果您尝试了上述解决方案仍然无法解决问题,请查看您的TextView xml,并确保删除android:background(因为它可能会覆盖您的et.setBackGroundColor(R.color.yourcolor))。

-11

在 XML 文件中声明文本视图时,您可以使用 android:textColor= " 您想要设置的任何文本颜色。


编程上...不是XML - aLL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接