【问题标题】:Change font size for an AlertDialog message [duplicate]更改 AlertDialog 消息的字体大小 [重复]
【发布时间】:2013-02-28 00:27:10
【问题描述】:

我正在尝试更改AlertDialog 消息的字体大小。

Button submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                Application1GoodExample.this);
        builder.setMessage("Your form has been successfully submitted");
        TextView textView = (TextView) findViewById(android.R.id.message);
        textView.setTextSize(40);
        builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();             
            }
        });

        builder.show();
    }
});

我收到一条错误消息,指出“findViewById()”对于 AlertDialog.Builder 类型未定义。

【问题讨论】:

    标签: android fonts android-ui android-alertdialog


    【解决方案1】:

    使用这个:

    AlertDialog alert = builder.create();
    alert.show();
    
    TextView msgTxt = (TextView) alert.findViewById(android.R.id.message);   
    msgTxt.setTextSize(16.0);
    

    在你的情况下:

    Button submit = (Button) findViewById(R.id.submitButton);
    
    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);
    
            builder.setMessage("Your form has been successfully submitted");
            builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();             
                }
            });
    
            // this will solve your error
            AlertDialog alert = builder.create();
            alert.show();
            alert.getWindow().getAttributes();
    
            TextView textView = (TextView) alert.findViewById(android.R.id.message);
            textView.setTextSize(40);
            Button btn1 = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
            btn1.setTextSize(16);
        }
    });
    

    如果这对您没有帮助,请在您的问题中发布您的 LogCat 错误。

    【讨论】:

    • 由于某种原因,当我按下提交按钮以显示警告对话框消息时,我的程序崩溃了
    • 我编辑了我的答案。错误,因为您使用了builder.show();
    • 这非常有效!太感谢了!我也在尝试更改“退出”按钮的大小,但由于我对 Android 很陌生,我无法弄清楚如何正确地做到这一点。
    • 我编辑了我的答案以实现你想要的。
    • 如何对标题做同样的事情?
    【解决方案2】:

    试试这个

     AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
     TextView textView = (TextView) dialog.findViewById(android.R.id.message);
     textView.setTextSize(40);
    

    【讨论】:

      【解决方案3】:

      findViewById方法属于View类型。

      AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);
      builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
          dialog.cancel();           
          } 
      });
      builder.setMessage("Your form has been successfully submitted");
      AlertDialog theDialog=builder.create();
      TextView textView = (TextView) theDialog.findViewById(android.R.id.message);
      textView.setTextSize(40);
      
      theDialog.show();
      

      【讨论】:

      • 由于某种原因,当我按下提交按钮以显示警告对话框消息时,我的程序崩溃了
      • 如果你向我们展示 logcat 的输出 :)
      • 我使用了 AwadKab 的解决方案并设法更改了警报消息的大小而没有出现任何错误。你知道我是否可以为“退出”按钮做同样的事情吗?我对 Android 开发非常陌生,无法弄清楚如何做到这一点。谢谢。
      • 如果你想自定义对话框的元素,最好的方法是为你的对话框创建一个全新的视图。看看这个:mkyong.com/android/android-custom-dialog-example
      • 我会调查的。非常感谢您的帮助!
      【解决方案4】:

      由于你没有使用CustomDialog,我建议你添加TextView如下,

      TextView textView = (TextView) findViewById(android.R.id.message); // remove builder object
      

      【讨论】:

      • 这行得通,谢谢。但是,当我按下提交按钮时,我的程序崩溃了。我已经更新了上面的代码。
      猜你喜欢
      • 2011-09-27
      • 2014-10-13
      • 2011-11-20
      • 1970-01-01
      • 2023-03-22
      • 2016-11-27
      • 2015-07-06
      • 2021-10-30
      • 2013-03-17
      相关资源
      最近更新 更多