Android:如何以编程方式在对话框上设置Button的文本大小?

Bal*_*ool 1 android button font-size android-alertdialog

我正在编写一个Android应用程序,我需要以编程方式在Dialog窗口按钮上放置更大的文本.

我看到有两个选项可以设置(正,负或中性)按钮AlertDialog.Builder:

  1. setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)

  2. setPositiveButton(int textId, DialogInterface.OnClickListener listener)

在选项1中,我只能设置文本,在选项2中,我可以使用文本值的资源ID.但是没有一个选项允许我添加一个样式按钮.

还有其他方法吗?

Car*_*ino 11

您无需创建自定义视图.

对话框构建器create方法返回一个AlertDialog对象,使您可以访问按钮.

    AlertDialog.Builder alert = new AlertDialog.Builder(YOUR_CONTEXT);
    AlertDialog dialog = alert.create();
    // Positive
    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(THE_SIZE);
    // Negative
    dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(THE_SIZE);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答.这非常有效.但有一点(对于将要使用此解决方案的人),`AlertDialog.getButton()`给出null.此链接[http://stackoverflow.com/questions/4604025/alertdialog-getbutton-method-gives-null-pointer-exception-android]提供了有关如何避免这种情况的更多信息. (6认同)