AlertDialog 不包装内容

AlertDialog Doesn't wrap Content

我的 alertDialog 没有包装它的内容,我尝试了很多解决方案但没有用我改变了样式,使用了 Dialog 和 ... 我希望 AlertDialog 包装其内容。

我明白了

但我想要这个

自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/settings_ll"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="right"
              android:padding="5dp"
              android:layout_margin="10dp"
    >
    <TextView
        android:id="@+id/font_selection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="انتخاب قلم:"
        android:textColor="#000000"
        android:padding="10dp"
        android:background="#eeeeee"/>
    <Spinner
        android:id="@+id/font_selection_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:spinnerMode="dropdown"
        />
    <TextView
        android:id="@+id/size_selection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="اندازه قلم"
        android:textColor="#000000"
        android:padding="10dp"
        android:background="#eeeeee"/>
    <LinearLayout
        android:id="@+id/select_font_size_ll"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/settings_small_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ریز"/>
        <Button
            android:id="@+id/settings_normal_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="معمولی"/>
        <Button
            android:id="@+id/settings_big_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="درشت"/>
    </LinearLayout>
    <TextView
        android:id="@+id/sample_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="این یک جمبه ی نمونه است"
        />


AlertDialog.java:

public class SettingsDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View view = inflater.inflate(R.layout.settings_layout, null);
        builder.setView(view);

        return builder.create();
    }
}

将您的父级衬里布局高度和宽度设置为 match_parent 并尝试:

      ?xml version="1.0" encoding="utf-8"?>
           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/settings_ll"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"

在android对话框中,通常采用宽度最小的视图的宽度。将 Spinner 的 layout_width 属性设置为 match_parent,您将获得所需的结果。

您不能指望在父级宽度为 wrap_content 的子级上具有 match_parent。所以将你的主要布局宽度设置为 match_parent 你的微调器也需要 match_parent 宽度检查我的例子:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title Text"
        android:gravity="right"
        android:textSize="20sp"
        android:padding="10dp"/>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="50dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Second Title"
        android:gravity="right"
        android:textSize="20sp"
        android:padding="10dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"/>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text="Bottom text"
        android:gravity="center"/>

</LinearLayout>