通过反射的方式来更改dialog中文字大小、颜色


private AlertDialog normalDialog;

private void showNormalDialog() {
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
*/
if (normalDialog == null) {
normalDialog = new AlertDialog.Builder(SceneListActivity.this).setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do

}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do

}
}).create();
}
normalDialog.setCancelable(false);
normalDialog.setTitle("提示");
normalDialog.setMessage("你确定要退出吗?");
// 显示
normalDialog.show();
normalDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE).setTextSize(20);
normalDialog.getButton(android.app.AlertDialog.BUTTON_NEGATIVE).setTextSize(20);
try {
//通过反射的方式来更改dialog中文字大小、颜色
Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object mAlertController = mAlert.get(normalDialog);

Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
mMessage.setAccessible(true);
TextView mMessageView = (TextView) mMessage.get(mAlertController);
mMessageView.setTextSize(23);
mMessageView.setTextColor(Color.RED);
Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView"); mTitle.setAccessible(true); TextView mTitleView = (TextView) mTitle.get(mAlertController); mTitleView.setTextSize(20);
mTitleView.setTextColor(Color.RED);
} catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); }}

声明:阿辉|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 通过反射的方式来更改dialog中文字大小、颜色


每一行代码都有它的故事