我如何设置一个TextView的文本颜色#bdbdbd编程?
#bdbdbd
使用. .
Color.parseColor("#bdbdbd");
就像,
mTextView.setTextColor(Color.parseColor("#bdbdbd"));
或者如果你在资源的color.xml文件中定义了颜色代码
color.xml
(From API >= 23)
mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));
(适用于API <23)
mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));
TextView tt; int color = Integer.parseInt("bdbdbd", 16)+0xFF000000; tt.setTextColor(color);
也
tt.setBackgroundColor(Integer.parseInt("d4d446", 16)+0xFF000000);
tt.setBackgroundColor(Color.parseColor("#d4d446"));
看到的:
Java/Android字符串到颜色转换
yourTextView.setTextColor(color);
或者,在你的情况下:yourTextView.setTextColor(0xffbdbdbd);
yourTextView.setTextColor(0xffbdbdbd);
伟大的答案。添加一个,从Android资源xml中加载颜色,但仍然以编程方式设置:
textView.setTextColor(getResources().getColor(R.color.some_color));
请注意,在API 23中,getResources().getColor()已弃用。使用:
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) 代替。< / p >
检查这。