我如何设置一个TextView的文本颜色#bdbdbd编程?
当前回答
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字符串到颜色的转换
其他回答
使用. .
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) 代替。
检查这个。
推荐文章
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML