【问题标题】:How to set Alert text size in alert dialog如何在警报对话框中设置警报文本大小
【发布时间】:2013-09-17 00:21:11
【问题描述】:

对于具有小屏幕的特定设备,默认警报消息太大,我想将其设置为自定义 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();
                }
            };

如何设置提示信息的文字大小?

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    编辑 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>"));
    

    注意:您可以使用更多的big's来获得更大的文字

    【讨论】:

    • 我还没有尝试过这个解决方案,但问题是带有消息的 TextView 和 Alert 的其他元素之间没有边距
    • 您可以为此创建一个 dialog.xml 并像这样将其添加到您的视图中
    • 如果不膨胀 xml,就没有办法做到这一点吗?
    • @AndreaF 接受您的回答,根据您的需要更改文字大小
    • @AndreaF 查看更新的答案,它将解决您的问题,如果有帮助,请接受它
    【解决方案2】:

    试试这个:

    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); 失败并返回 NPETextView 的 id 显然是 not android.R.id.message。进一步的建议?
    • 尝试点击此链接:stackoverflow.com/questions/26455919/…解释如何设置对话框样式。
    【解决方案3】:

    使用此代码控制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 { *; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      相关资源
      最近更新 更多