如何在警告对话框中设置警告文本的大小

4

默认情况下,警告信息在小屏幕设备上太大了,我想将其设置为自定义的dp。

我的警告框大概是这样的:

OnClickListener addNewItemListener = new OnClickListener() {
                public void onClick(View v) {
                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            MyActivity.this);

                    LinearLayout myLayout= new LinearLayout(MyActivity.this);
                    myLayout.setOrientation(LinearLayout.VERTICAL);

                    alert.setTitle(R.string.add_title);
                    alert.setMessage(R.string.add_message);

                    final TextView t1 = new TextView(MyActivity.this);
                    t1.setText("Name");
                    final EditText input1 = new EditText(MyActivity.this);
                    myLayout.addView(t1);
                    myLayout.addView(input1);
                    alert.setView(myLayout);
                    alert.setPositiveButton(R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                }
                            });
                    alert.setNegativeButton(R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    try {
                                        ....
                                    } catch (RuntimeException e) {
                                        Alerts.DatiErrati(MyActivity.this);
                                    }

                                }
                            });
                    alert.show();
                }
            };

我该如何设置警告消息的文本大小?

3个回答

6
编辑2:
这是您可以选择的最佳方法。
AlertDialog.Builder builder = new Builder(this);
builder.setMessage("Record and Images will be deleted?")
        .setTitle("MyTitle")
        .setCancelable(true)
        .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener()
                        {

                            public void onClick(DialogInterface dialog,
                                    int id)
                            {

                                dialog.cancel();
                                finish();
                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener()
                        {

                            public void onClick(DialogInterface dialog,
                                    int id)
                            {
                                dialog.cancel();
                            }
                        });
        AlertDialog dialog = builder.create();
        dialog.show();
        TextView textView = (TextView) dialog.findViewById(android.R.id.message);
        textView.setTextSize(40);

您可以使用以下内容来仅获取更大的文本。
  alert.setMessage(Html.fromHtml("<Big>"+getString(R.string.add_message)+"</Big>"));

注意: 你可以使用更多的大号字体来获得更大的文本效果。


我还没有尝试过这个解决方案,但问题在于TextView与消息之间以及Alert的其他元素之间没有边距。 - AndreaF
你可以为此创建一个dialog.xml文件,并像这样将其填充到你的视图中。 - Trikaldarshiii
有没有不需要解析 XML 的方法来完成这个操作? - AndreaF
@AndreaF,根据您的需要更改文本大小。 - Trikaldarshiii
@AndreaF 请查看更新后的答案,它将解决您的问题。如果有帮助,请接受它。 - Trikaldarshiii

1

试试这个:

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

不幸的是,textView.setTextSize(40); 出现了 NPE 错误。 显然,TextView 的 ID 不是 android.R.id.message。 还有其他建议吗? - Narayana J
1
请尝试访问以下链接:https://dev59.com/6F8d5IYBdhLWcg3wxUjC 该链接解释了如何为您的对话框添加样式。 - Evgeni Roitburg

0

使用此代码来控制AlertDialog

  AlertDialog builder = new AlertDialog.Builder(getActivity())
                    .setTitle("title")
                    .setMessage("message")
                    .setPositiveButton(getResources().getString(R.string.yes), (dialogInterface, i) -> {
                        dialogInterface.dismiss();
                    })
                    .setNegativeButton(getResources().getString(R.string.cancel), (dialogInterface, i) -> dialogInterface.dismiss())


            try {

//                          /Modify Determine the font size of the Cancel button
//                builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(26);
//                builder.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(26);

                // Get the mAlert object
                Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
                mAlert.setAccessible(true);
                Object mAlertController = mAlert.get(builder);

                // Get mTitleView and set the size of the color
                Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
                mTitle.setAccessible(true);
                TextView mTitleView = (TextView) mTitle.get(mAlertController);
                mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen._13sdp));
                mTitleView.setTextColor(getResources().getColor(R.color.font_black));
                mTitleView.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8f, getResources().getDisplayMetrics()), 1.0f);
                mTitleView.setGravity(View.TEXT_ALIGNMENT_VIEW_START);
                mTitleView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);

                // Get mMessageView and set the size of the color
                Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
                mMessage.setAccessible(true);
                TextView mMessageView = (TextView) mMessage.get(mAlertController);
                mMessageView.setPadding(mMessageView.getPaddingLeft(), (int) (mMessageView.getCompoundPaddingTop() + (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 7, getResources().getDisplayMetrics()))), mMessageView.getCompoundPaddingRight(), mMessageView.getCompoundPaddingBottom());
                mMessageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen._10sdp));
                mMessageView.setTextColor(getResources().getColor(R.color.font_black2));
                mMessageView.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8f, getResources().getDisplayMetrics()), 1.0f);
                mMessageView.setGravity(View.TEXT_ALIGNMENT_VIEW_START);
                mMessageView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
  


            } catch (Exception e) {
                e.printStackTrace();
            }

使用以下代码进行Proguard

# for alertDialog, to customize title and message
-keep class androidx.appcompat.app.AlertDialog { *; }
-keep class androidx.appcompat.app.AppCompatDialog { *; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接