【发布时间】:2013-02-10 19:57:02
【问题描述】:
我尝试通过使用此功能更改 AlertDialog 字体
private void saveDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(res.getString(R.string.dialog_title))
.setMessage(res.getString(R.string.dialog_saveconfirm))
.setCancelable(false)
.setNegativeButton(res.getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setPositiveButton(res.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do some thing
});
AlertDialog alert = builder.create();
alert.show();
TextView tit = (TextView) alert.findViewById(android.R.id.title);
TextView msg = (TextView) alert.findViewById(android.R.id.message);
Button btn2 = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
Button btn1 = alert.getButton(DialogInterface.BUTTON_POSITIVE);
tit.setTypeface(UtiliShare.getTf());
msg.setTypeface(UtiliShare.getTf());
btn1.setTypeface(UtiliShare.getTf());
btn2.setTypeface(UtiliShare.getTf());
}
当我在 Activity 中调用函数时,我在设置字体时为 tit 设置了 02-25 17:59:04.759: E/AndroidRuntime(1014): java.lang.NullPointerException,但是当我删除 tit 对话框时效果很好。
我认为TextView tit = (TextView) alert.findViewById(android.R.id.title); 中的错误是返回 null。
我该如何解决这个问题??
更新 此链接包含我的问题的答案Answer
感谢Sam
【问题讨论】:
-
我不相信Android会让你像这样访问这些TextViews,但你应该能够使用样式和主题来做你想做的事:Change the style of AlertDialog
-
我的帖子可能对你有帮助stackoverflow.com/questions/4025605/…"
-
嗯,这个答案声称您可以使用
alertTitle而不是title:Changing font size into an AlertDialog。 (我自己没试过。) -
感谢 Sam,它现在为我工作。你拯救了我的一天。
-
@Sam。 alertTitle 答案适用于获取视图,但仍然存在我在答案中指出的问题。
标签: android android-alertdialog