通过findViewById获取AlertDialog标题



我试图通过使用这个函数来更改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());
}

当我在"活动"中调用函数时,当设置字体时,tit使用02-25 17:59:04.759: E/AndroidRuntime(1014): java.lang.NullPointerException,但当我删除tit对话框时,效果良好。

我认为TextView tit = (TextView) alert.findViewById(android.R.id.title);中的错误是返回null。

我该如何解决这个问题??


更新此链接包含我的问题答案答案

感谢Sam

为了以您想要的方式更改文本视图,我建议使用自定义对话框布局。看看

http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

如果使用android.support.v7.app.AlertDialog,则必须使用android.support.v7.appcompat.R.id.alertTitle,否则使用android.R.id.title

最新更新