AlertDialog更改标题颜色,字体等
2014-08-22 09:54
423 查看
更改AlertDialog标题的方法google目前没有提供,只能通过其他办法
一种办法是:首先在源代码中找到有个叫AlertController的类,这个类就是AlertDialog的实现类,是没有对外公开的,然后在这个类中有个私有成员变量叫mTitleView,这个就是AlertDialog的title的TextView,所以只要得到这个成员变量的实例,即可自定义AlertDialog的title
得到这个的实例变量的方法通过两步反射来实现,如下:
[java] view
plaincopy
AlertDialog dialog = (AlertDialog) getDialog();
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);
title.setTextColor(0xff33b5e5);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
[java] view
plaincopy
AlertDialog dialog = (AlertDialog) getDialog();
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);
title.setTextColor(0xff33b5e5);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
当然还有其他办法,比如直接把title隐藏掉,然后在content View中自定义一个title出来等等
一种办法是:首先在源代码中找到有个叫AlertController的类,这个类就是AlertDialog的实现类,是没有对外公开的,然后在这个类中有个私有成员变量叫mTitleView,这个就是AlertDialog的title的TextView,所以只要得到这个成员变量的实例,即可自定义AlertDialog的title
得到这个的实例变量的方法通过两步反射来实现,如下:
[java] view
plaincopy
AlertDialog dialog = (AlertDialog) getDialog();
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);
title.setTextColor(0xff33b5e5);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
[java] view
plaincopy
AlertDialog dialog = (AlertDialog) getDialog();
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);
title.setTextColor(0xff33b5e5);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
当然还有其他办法,比如直接把title隐藏掉,然后在content View中自定义一个title出来等等
相关文章推荐
- Android AlertDialog更改标题颜色,字体等
- AlertDialog更改标题颜色,字体等
- IOS 如何更改导航栏样式及状态栏字体颜色(导航栏背景、前景、标题及返回字体颜色)
- AlertDialog更改标题颜色,字体等
- AlertDialog更改标题颜色,字体等
- android:更改PagerTabStrip背景颜色,标题字体样式、颜色和图标,以及指示条的颜色
- AlertDialog修改标题、内容、按钮的颜色和字体大小
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- AlertDialog更改标题颜色,字体等
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等
- tabbar Item图片原色显示以及标题字体大小颜色控制
- Eclipse代码字体、颜色美化,更改字体大小、颜色
- 更改ListView字体颜色
- c# GroupBox 只修改标题的字体和颜色
- IOS中修改导航默认标题颜色、字体
- android中为TextView动态改变字体颜色,如点击时更改
- 更改Textiew背景色和字体颜色
- 设置导航栏标题,字体颜色,大小 背景颜色 等
- win7字体怎么改?win7更改默认字体与默认颜色方法图解