将字体大小更改为AlertDialog

Waz*_*_Be 36 android font-size textview android-alertdialog

我试图将一些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();
Run Code Online (Sandbox Code Playgroud)

问题:布局不滚动

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

message.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)

问题:布局是滚动,bute没有"惯性"(不知道如何调用它......但我想你明白了.)

解决方法3)使用Scrollview.

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

Gre*_*ory 110

实际上,您可以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);
Run Code Online (Sandbox Code Playgroud)

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

  • 实际上你也必须调用`show()`,否则没有TextView.如果你想缩放按钮,请使用`android.R.id.button1`等 (10认同)
  • 好吧,文本 textView 对象似乎为空 (4认同)
  • 在构建对话框时必须调用setMessage(),否则首先不会有消息textview. (2认同)

Sil*_*ria 25

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

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

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

  • 这两种获取ID的方法之间有什么区别?为什么没有android.R.id.alertTitle? (2认同)
  • 该解决方案似乎不适用于最新更新。findViewById()始终返回null。:-( (2认同)

ken*_*u73 5

这是我的解决方案...您需要创建滚动容器,然后将 TextView 添加到 ScrollView 中,就像在 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);
Run Code Online (Sandbox Code Playgroud)


M. *_*han 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();
Run Code Online (Sandbox Code Playgroud)