AlertDialog 修改文本内容的颜色
来源:互联网 发布:武汉大学法律硕士 知乎 编辑:程序博客网 时间:2024/05/16 05:57
最近遇到一个问题在联想A858T白色手机上测试如下AlertDialog时,AlertDialog背景默认为白色,title 、message为黑色,但是CheckBox的Text却为白色。
final CheckBox cb = new CheckBox(this); cb.setChecked(false); cb.setText(getResources().getString(R.string.close_wifi_switch)); dialog = new AlertDialog.Builder(this) .setTitle(getResources().getString(R.string.exit_wimo_sure)) .setView(cb) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Do nothing. } }) .setPositiveButton(R.string.exit, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).create(); dialog.show();
故想到是否可以用反射方法读取到AlertDialog的title颜色值,并将其赋给CheckBox的TextColor,后在网上找到相关的AlertController类,这个类是AlertDialog的实现类,是没有对外公开的,然后在这个类中有个私有成员变量叫mTitleView,这个就是AlertDialog的title的TextView,所以只要得到这个成员变量的实例,即可得到title的颜色值
dialog.show();// 很重要,在执行下列操作之前一定要先执行 try { Field mAlert = AlertDialog.class.getDeclaredField("mAlert"); mAlert.setAccessible(true); Object alertController = mAlert.get(dialog); Field mTitleView = alertController.getClass().getDeclaredField( "mTitleView"); mTitleView.setAccessible(true); TextView title = (TextView) mTitleView.get(alertController); ColorStateList colorStateList = title.getTextColors(); cb.setTextColor(colorStateList); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
类似于修改checkBox的Text颜色,也可以直接修改AlertDialog的title字体颜色,title.setTextColor(XXXXX)
0 0
- AlertDialog 修改文本内容的颜色
- AlertDialog修改标题、内容、按钮的颜色和字体大小
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- Android修改AlertDialog的背景颜色
- 修改 AlertDialog 中按钮颜色的方法
- 能修改按钮字体颜色的AlertDialog
- 通过源码分析,修改AlertDialog按钮的颜色
- VC中修改STATIC的文本颜色
- 修改textField默认值的文本颜色
- python修改文本颜色
- ATL中修改文本控件的文本颜色
- ext GridPanel2 修改列的文本内容
- C#修改txt文本内容的方法
- dwz javascript 修改 input的文本内容
- dwz javascript 修改 input的文本内容
- AlertDialog的样式修改
- 封装Android加载网络图片
- 标准模式与怪异模式的共存缘由及其使用
- 一张图了解 Java 注解(Annotation)
- Android Volley完全解析(三),定制自己的Request
- servlet中跳转页面的方法和区别
- AlertDialog 修改文本内容的颜色
- 浅谈关于字符串中求字符频率问题
- 日志组件logback的介绍及配置使用方法
- 列控系统 CTCS(重点是CTCS-2)
- static web project与dynamic web project
- 工作之杂记--use jQueryUI
- JSTL标签库_1_cout_cset_cremove_ccatch
- DHCP租约
- 关于Android屏幕解锁与持续开亮