Android - 如何使 AlertDialog 比标准更窄?

Android - How to make an AlertDialog narrower than standard?

我目前有以下代码来构建带有 ProgressBar 的等待对话框:

LayoutInflater factory = LayoutInflater.from(TherapistActivity.this);
View view = factory.inflate(R.layout.waitdialog, null);
dialog = new AlertDialog.Builder(TherapistActivity.this)
    .setView(view)
    .setCancelable(false)
    .create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

wmlp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
wmlp.x = 0;   //x position
wmlp.y = Math.round(metrics.density * 100);   //y position

wmlp.width = Math.round(metrics.density * 55);  //doesn't appear to work

这是我的对话的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/top"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:background="@drawable/boxbkg">

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"/>


</RelativeLayout>

我希望我的对话框是一个只有旋转 ProgressBar 的小方形对话框。但是,即使使用 wmlp.width = Math.round(metrics.density * 55),对话框仍然很宽。

解决这个问题的正确方法是什么?

据我所知,更改对话框 - 参数/宽度和高度 - 应该在 onCreate(需要设置样式)和 onCreateDialog(设置参数)中进行。

来自自定义 DialogFragment 的这两种方法的示例,它在整个屏幕上发生,没有任何边距:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    Window w = dialog.getWindow();
    w.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
    WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
    params.verticalMargin = 0;
    params.horizontalMargin = 0;
    params.x=0;
    params.y = 0;
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.width= ViewGroup.LayoutParams.MATCH_PARENT;
    params.height = ViewGroup.LayoutParams.MATCH_PARENT;
    w.setGravity(Gravity.TOP| Gravity.LEFT);

    return dialog;
}

如果对你有帮助,请告诉我。

我明白了。看来我还需要两行代码。在设置宽度之前,我需要这行代码:

wmlp.copyFrom(dialog.getWindow().getAttributes());

设置宽度后,我需要这行代码:

dialog.getWindow().setAttributes(wmlp);

原始代码与答案中的额外代码相结合,不幸的是不适用于我的代码,这可能是由于使用 Fragment.

以下代码(使用 Java 8/min API 24/target API 27 进行测试)适用于 Fragment 以及纵向和横向屏幕方向.它允许您将对话框的大小设置为您喜欢的任何大小(见下文)并且无需设置其位置,因为最终它仍然只是一个普通的 AlertDialog(没有按钮或标题)和自定义 view:

AlertDialog.Builder builder = new AlertDialog.Builder((AppCompatActivity) getActivity());
builder.setView(R.layout.progress_view);
AlertDialog loadingDialog = builder.create();
loadingDialog.show();
loadingDialog.getWindow().setLayout(400,400); //You have to call this after "show"!
//loadingDialog.dismiss();

我的progress_view布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#FF0000"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="20dp"
    android:paddingEnd="20dp"
    android:paddingStart="20dp"
    android:paddingTop="20dp">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

我之前试过使用LinearLayout但是没有用,对话框仍然使用它的原始大小。这里重要的一点是 match_parent,因为只有这样才能真正使 AlertDialog 中的进度圈居中。使用 wrap_content 会将其推到对话框的左上角。

结果如下所示:

您当然可以添加更多内容,例如a "Please wait..." TextView 或将背景颜色设置为您想要的任何颜色,我注意到两件事:

  1. 透明度不起作用。如果您将背景设置为例如50% alpha,透过它可以看到AlertDialog的白色背景。我尝试按照建议 here(自定义样式或 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)))更改对话框的透明度,但是,即使它有效,对话框也最终失去了圆角并被压扁。
  2. 您必须测试适合您的尺码。 400 x 400 对于默认进度圈和一些填充没问题,但如果对话框对于视图内容来说太小,它根本不会显示。