将字体大小更改为AlertDialog

时间:2011-07-03 12:39:38

标签: android textview alertdialog font-size

我正在尝试将一些loooong文本放入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());

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

解决方法3)使用Scrollview。

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

7 个答案:

答案 0 :(得分:108)

您实际上可以非常轻松地访问邮件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存在风险。

答案 1 :(得分:25)

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

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

...确保我在nullsetTitleFont方法中检查setBodyFont

答案 2 :(得分:5)

按钮:

  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();

答案 3 :(得分:4)

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

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);

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

这可以在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();

答案 6 :(得分:0)

               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();