Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将字体大小更改为AlertDialog_Android_Textview_Android Alertdialog_Font Size - Fatal编程技术网

Android 将字体大小更改为AlertDialog

Android 将字体大小更改为AlertDialog,android,textview,android-alertdialog,font-size,Android,Textview,Android Alertdialog,Font Size,我正在尝试将一些长文本放入AlertDialog。 唯一的问题是默认字体太大了,所以我想把它缩小 以下是我尝试的所有解决方法及其问题 解决方法1)使用TextView和myView.setTextSize(12) 问题:布局未滚动 解决方法2)使文本视图可滚动 message.setMovementMethod(LinkMovementMethod.getInstance()); 问题:布局是滚动的,但没有“惯性”(不知道怎么称呼它……但我想你明白。) 解决方法3)使用滚动视图 这正是我要尝试

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

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

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

问题:布局未滚动

解决方法2)使文本视图可滚动

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

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


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

您实际上可以非常轻松地访问邮件的
文本视图,然后更改其大小。我测试了一个更大的尺寸,但是你可以用任何你想要的尺寸。文本将很好地滚动,就像它已经做的那样。视图的
id
android.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
的风险

这是我的解决方案。。。您需要创建滚动容器,然后像在XML布局中一样在滚动视图中添加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);

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

final int-alertTitle=ctx.getResources().getIdentifier(“alertTitle”、“id”、“android”);
setTitleFont((TextView)dlg.findViewById(alertTitle));
setBodyFont((TextView)dlg.findviewbyd(android.R.id.message));
…确保在我的
setTitleFont
setBodyFont
方法中检查
null

按钮:

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

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


这将在2020年起作用。我已经在安卓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();

嗯,文本文本视图对象似乎是空的。在构建对话框时必须调用setMessage(),否则首先就不会有消息文本视图。实际上,您也必须调用
show()
,否则就不会有文本视图。如果您还想缩放按钮,请使用android.R.id.button1
等了解为什么textView可以为空。如果只有
AlertDialog.Builder
对象,如何更改该字体?这对获取标题视图很有效(谢谢!),但一旦获得它,我只能设置两种字体:标准字体和一种小字体(值为11.0)。这对我将字体从assets文件夹设置为警报框的标题起到了作用。很好!非常感谢!使用它,我可以将标题字体大小更改如下:int-alertTitle=context.getResources().getIdentifier(“alertTitle”,“id”,“android”);View title=dialog.findViewById(alertTitle);如果(title!=null&&title instanceof TextView){((TextView)title).setTextSize(14);}这两种获取id引用的方法有什么区别。为什么没有android.R.id.alertTitle?此解决方案似乎不再适用于最近的更新。findViewById()始终返回null.:-(不确定为什么这没有得到任何支持票。这可能不是OP直接问题的答案,但一旦你把消息变大/变小,你可能会想把按钮文本变大/变小。这看起来是一个很好的、自然的(好的,至少是javascript-y)如果必须在运行时完成任务,那么这是一种完成任务的方法。我将它与已接受的正确答案()结合起来,并将这两行放在侦听器中。另外,不必为getDimen调用费心,只需设置一个大小。别忘了声明ad final!
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();
               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();