【问题标题】:How to resize an AlertDialog.Builder?如何调整 AlertDialog.Builder 的大小?
【发布时间】:2016-08-15 03:01:26
【问题描述】:

我想调整我的AlertDialog.Builder 的大小,但我在这个网站上没有找到任何解决方案,我尝试了我在this one 等问题上看到的所有内容,但似乎比我正在寻找的要旧,我的代码我现在看不下去了……

ALERTDIALOGBUILDER.JAVA

public class AlertDialogInfo extends AlertDialog.Builder {

public AlertDialogInfo(Context context, String title, String msg) {
    super(context);
    AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.AppCompatAlertDialogStyle);
    builder.setTitle(title);
    builder.setMessage(msg);
    builder.setNegativeButton("OK",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    }
}

我尝试将AlertDialog.Builder 变为AlertDialog 来调整它的大小,但只是显示一个没有数据的黑色方块。

问题:我应该怎么做才能制作带有大文本的 AlertDialog.Builder? (需要确定滚动,因为我不想让对话框和我的屏幕一样大)

更新

我想要的是这样的,我必须通过Bundle 更改文本,我从使用FrameLayout 的 AppCompatActivity 调用 AlertDialog(我不知道应该是哪种对话框来涵盖什么我需要):

更新 2

最后我一直在尝试用下一个代码做我正在寻找的东西,但它仍然不起作用......

ALERT_DIALOG.XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="250dp">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical">

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

</LinearLayout>

MYALERTDIALOG.JAVA

public class MyDialogFragment extends DialogFragment {

public static MyDialogFragment newInstance(String text) {
    MyDialogFragment frag = new MyDialogFragment();
    Bundle args = new Bundle();
    args.putString("TEXT", text);
    System.out.print("Text dentro de newInstance: " + text);
    frag.setArguments(args);
    return frag;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String sinopsis = getArguments().getString("TEXT");
    System.out.print("Text dentro de onCreateDialog: " + text);
    return new AlertDialog.Builder(getActivity())
            .setIcon(android.R.drawable.ic_dialog_info)
            .setTitle("TITLE")
            .setMessage(text)
            .setNegativeButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dismiss();
                        }
                    }
            ) .create();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.alert_dialog, container);
}
}

ActivityWhereDialogIsBeenCalled.java

........
DialogFragment frag = MyDialogFragment.newInstance("MY MESSAGE");
        frag.show(getSupportFragmentManager(),"TAG");
...............

我的对话框使用默认布局显示(它没有滚动视图,也没有我感兴趣的大小(仅显示为默认的 android 警报对话框...)

我该如何解决这个问题?我认为这很愚蠢,但令人沮丧......无论如何,谢谢!

【问题讨论】:

标签: android resize android-alertdialog


【解决方案1】:

你应该使用自定义对话框布局,检查documentation

实际上,您可以通过getDialog().getWindow().setLayout(width, height); 调整对话框大小,只需调用它 onResume(而不是 onCreateView)。

您的视图必须嵌套在 ScrollView 中,请查看post

在对话框中阅读大量文本可能很烦人,最好使用所有像素,考虑简单的 Activity/Fragment。

【讨论】:

  • 谢谢你,Maxim,你能告诉我如何使非全屏的 DialogFragment 吗?似乎是同样的错误...
  • 我已经看过这篇文章,并且似乎是我正在寻找的东西,谢谢。但是,自从 Dialog 调用的 Activity 以来,使用 Bundle 更改对话框消息的最佳方法是什么?
  • 与任何 Fragment 相同,repo link
  • 我试过 dislikedialog... 但无法缩小我的对话框片段... 它仍然填充与文本一样大
【解决方案2】:

如果您在 DialogFragment 中使用 AlertDialog,并使用 AppCompat 支持库 23.2.1 或更高版本,则必须将调整大小代码放入 DialogFragment.onStart(),否则将不起作用.像这样的:

@Override
public void onStart() {
    super.onStart();
    // Method 1
    alertDialog.getWindow().getAttributes().width = 400; // pixels
    alertDialog.getWindow().getAttributes().height = 500; // pixels
    // Re-apply the modified attributes
    alertDialog.getWindow().setAttributes(
           alertDialog.getWindow().getAttributes());
    
    // Method 2 (alternative)
    alertDialog.getWindow().setLayout(400, 500); // size in pixels
}

更多信息:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多