【问题标题】:Changing font size into an AlertDialog将字体大小更改为 AlertDialog
【发布时间】:2011-09-27 14:55:20
【问题描述】:

我正在尝试将一些冗长的文本放入 AlertDialog。 唯一的问题是默认字体太大了,所以我想把它变小。

以下是我尝试过的所有解决方法及其问题。

解决方法 1) 使用 TextView 和 myView.setTextSize(12);

final TextView myView = new TextView(getApplicationContext());
myView.setText(myLongText);
myView.setTextSize(12);
final AlertDialog d = new AlertDialog.Builder(context)
    .setPositiveButton(android.R.string.ok, null)
.setTitle(myTitle)
.setView(myView)
.create();

问题:布局不滚动

解决方法 2) 使 TextView 可滚动。

message.setMovementMethod(LinkMovementMethod.getInstance());

问题:布局在滚动,但没有“惯性”(不知道怎么称呼它。但我想你明白了。)

解决方法 3) 使用滚动视图。

这就是我要尝试的方法,但我不敢相信没有更简单的解决方案......

【问题讨论】:

    标签: android textview android-alertdialog font-size


    【解决方案1】:

    您实际上可以很容易地访问消息的TextView,然后更改它的大小。我用更大的尺寸进行了测试,但你可以使用任何你想要的尺寸。文本将像它已经滚动一样很好地滚动。视图的idandroid.R.id.message

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

    这可能是一个更清洁的解决方案,但我不确定TextView 是否存在null 的风险。

    【讨论】:

    • 好吧,文本 textView 对象似乎确实为空
    • 在构建对话框时必须调用setMessage(),否则一开始就没有消息textview。
    • 其实你也得调用show(),否则没有TextView。如果您还想缩放按钮,请使用 android.R.id.button1
    • 查看stackoverflow.com/a/18767270/2914140 了解为什么 textView 可以为空。
    • 如果只有AlertDialog.Builder对象,如何更改字体?
    【解决方案2】:

    我正在使用以下代码更改 AlertDialog 的标题和消息文本...

    final int alertTitle = ctx.getResources().getIdentifier("alertTitle", "id", "android");
    setTitleFont((TextView) dlg.findViewById(alertTitle));
    setBodyFont((TextView) dlg.findViewById(android.R.id.message));
    

    ...确保在我的 setTitleFontsetBodyFont 方法中检查 null

    【讨论】:

    • 这对获取标题视图很有用(谢谢!),但是一旦我得到它,我只能设置两种字体:标准字体和小字体(值为 11.0)。跨度>
    • 这对我有用,可以将资产文件夹中的字体设置为警报框的标题。不错!
    • 非常感谢!使用它,我可以如下更改标题字体大小: int alertTitle = context.getResources().getIdentifier("alertTitle", "id", "android");查看标题 = dialog.findViewById(alertTitle); if (title != null && title instanceof TextView) { ((TextView) title).setTextSize(14); }
    • 这两种获取id引用的方法有什么区别。为什么没有android.R.id.alertTitle?
    • 此解决方案似乎不再适用于最近的更新。 findViewById() 始终返回 null。 :-(
    【解决方案3】:

    对于按钮:

      final AlertDialog ad = new AlertDialog.Builder(mainScreen)
    .setPositiveButton("OK", null) 
    .setNegativeButton("Cancel", null).create();
    
    ad.setOnShowListener(new DialogInterface.OnShowListener() {
                                                @Override
                                                public void onShow(DialogInterface dialog) {
                                                    int textSize = (int) Helper.getDimen(mainScreen, R.dimen.textSize12);
                                                    ad.getButton(Dialog.BUTTON_POSITIVE).setTextSize(textSize);
                                                    ad.getButton(Dialog.BUTTON_NEGATIVE).setTextSize(textSize);
                                                }
    
    
                                          });
    
    ad.show();
    

    【讨论】:

    • 不知道为什么这没有得到任何支持。它可能不是对 OP 直接问题的回答,但是一旦您使消息变大/变小,您很可能希望使按钮文本变大/变小。如果无论如何都必须在运行时完成,这似乎是一种很好的、​​自然的(好吧,至少是 javascript-y)完成任务的方式。我将此与公认的好答案 (stackoverflow.com/a/6563075/165164) 结合起来,并将这两行放在侦听器中。此外,无需打扰 getDimen 调用,只需设置大小即可。不要忘记将广告声明为最终广告!
    【解决方案4】:

    这是我的解决方案...您需要创建滚动容器,然后像在 XML 布局中一样在 ScrollView 中添加 TextView。

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    String str = getString(R.string.upgrade_notes); 
    final ScrollView s_view = new ScrollView(getApplicationContext());
    final TextView t_view = new TextView(getApplicationContext());
    t_view.setText(str);
    t_view.setTextSize(14);     
    s_view.addView(t_view);
    alertDialog.setTitle("Upgrade notes!");
    alertDialog.setView(s_view);
    

    【讨论】:

      【解决方案5】:

      您可以通过使用 SpannableStringBuilder 而不是字符串来解决此问题。

      http://www.android--tutorials.com/2016/10/android-change-alertdialog-title-text_21.html

      【讨论】:

        【解决方案6】:

        这在 2020 年有效。我已经在 Android 10 中对此进行了测试

        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.YourStyle) //Style is only needed if you want to customize look&feel
                    .setTitle(R.string.title)
                    .setMessage(R.string.message)
                    .setPositiveButton(R.string.accept, null);
            AlertDialog alertDialog = builder.create();
            alertDialog.setOnShowListener(dialog -> {
                //The following is to style dialog message
                TextView message = alertDialog.findViewById(android.R.id.message);
                if (message != null) {
                    message.setTextSize(12);
                    message.setTextColor(getColor(R.color.yellow));
                }
                //The following is to style dialog title. Note that id is alertTitle
                TextView title = alertDialog.findViewById(R.id.alertTitle);
                if (title != null) {
                    title.setTextColor(getColor(R.color.black));
                }
            });
            alertDialog.show();
        

        【讨论】:

          【解决方案7】:
                         AlertDialog.Builder builder = new 
                          AlertDialog.Builder(YourActivity.this);
                          builder.setTitle("Your Title");
                          builder.setMessage("Your Message");
                          builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialogInterface, int i) {
                                  // Your Positive Function
                              }
                          });
                          builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialogInterface, int i) {
                                  // Your Negative Function
                              }
                          });
                          builder.show();
          

          【讨论】:

            猜你喜欢
            • 2014-10-13
            • 2011-11-20
            • 2013-02-28
            • 2023-03-22
            • 1970-01-01
            • 2014-07-18
            • 2021-03-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多