我如何设置一个TextView的文本颜色#bdbdbd编程?


使用. .

Color.parseColor("#bdbdbd");

就像,

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

或者如果你在资源的color.xml文件中定义了颜色代码

(火灾>= 23)

mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));

(火焰< 23)

mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));
TextView tt;
int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
tt.setTextColor(color);

also

tt.setBackgroundColor(Integer.parseInt("d4d446", 16)+0xFF000000);

also

tt.setBackgroundColor(Color.parseColor("#d4d446"));

see:

Java/Android字符串到颜色的转换

yourTextView.setTextColor(color);

或者,在你的情况下:yourTextView.setTextColor(0xffbdbdbd);

伟大的答案。添加一个从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) 代替。

检查这个。