如何通过编程将TextView的文本颜色设置为#bdbdbd
?
请使用 git branch
创建一个新的分支:
Color.parseColor("#bdbdbd");
就像这样,
mTextView.setTextColor(Color.parseColor("#bdbdbd"));
如果您已经在资源的color.xml
文件中定义了颜色代码,那么(从API >= 23开始)mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));
(针对 API < 23)
mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));
0xFFBDBDBD
? - Rudey很好的答案。在此补充一个选项,它可以从Android资源XML中加载颜色,但仍可以通过编程方式设置:
textView.setTextColor(getResources().getColor(R.color.some_color));
请注意,从API 23开始,getResources().getColor()
已被弃用。请改用以下方法:
textView.setTextColor(ContextCompat.getColor(context, R.color.some_color));
所需颜色在xml中定义为:
<resources>
<color name="some_color">#bdbdbd</color>
</resources>
更新:
这种方法在API 23级中被弃用。请改用getColor(int, Theme)。
查看此链接。
tv.setTextColor(color.holo_green_light)
。文本显示不出来。所以我在 XML 中将颜色设置为资源,如 @android:color/holo_green_light
,然后通过编程方式设置它。 - RTFyourTextView.setTextColor(color);
或者,在你的情况下:yourTextView.setTextColor(0xffbdbdbd);
0xff
是什么意思?setTextColor(0xffbdbdbd)
和 setTextColor(Color.parseColor("#bdbdbd"))
有什么区别? - mridparseColor
时,"#bdbdbd"是一个被解析为相同整数的字符串。 - JaveTextView tt;
int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
tt.setTextColor(color);
同样也
tt.setBackgroundColor(Integer.parseInt("d4d446", 16)+0xFF000000);
同样也
tt.setBackgroundColor(Color.parseColor("#d4d446"));
参见:
textView.setTextColor(textView.getContext().getColor(R.color.white));
将文本设置为白色或在 colors.xml 中指定的任何其他颜色。 - Ola Ström