DialogFragment不会尊重wrap_content

Sam*_*ern 8 android android-appcompat android-layout

我有一个自定义的DialogFragment类,如下所示:

/**
 * Dialog Fragment containing rating form.
 */
public class RatingDialogFragment extends DialogFragment {

    public static final String TAG = "RatingDialog";

    // ...


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_rating, container, false);
        ButterKnife.bind(this, v);

        return v;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

根视图是一个 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">
Run Code Online (Sandbox Code Playgroud)

每个孩子的观点都有android:layout_height="wrap_content".

但它看起来像这样.我能做错什么?

在此输入图像描述

Sam*_*ern 7

这部分是我布局中的问题。在对话框的底部,我有一个按钮栏:

<!-- Cancel and apply buttons -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button_cancel"
        style="@style/Base.Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/cancel"
        android:textColor="@color/greySecondary"
        android:theme="@style/ThemeOverlay.FilterButton" />


    <Button
        android:id="@+id/button_search"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/apply"
        android:theme="@style/ThemeOverlay.FilterButton" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

第一个View(间隔物)正在扩展以填充视口,并将其更改为此固定:

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)

我还必须将其添加到onResumeDialogFragment

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}
Run Code Online (Sandbox Code Playgroud)


Sor*_*shA 4

这似乎是布局和对话框片段的问题。改为手动设置:

@Override
public void onResume() {
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(your_value, your_value);
    window.setGravity(Gravity.CENTER);
}
Run Code Online (Sandbox Code Playgroud)

如果需要将片段中的内容包裹起来,可以遍历视图并总结总高度,然后将其用作布局高度。