DialogFragment不支持wrap_content属性

12

我有一个自定义的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;
    }

    // ...
}

根视图是一个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">

每个子视图都有android:layout_height="wrap_content"属性。

但是它看起来像这样。我做错了什么?

输入图像描述


每个组件都有最小高度。在我看来,它看起来是正确的。您能否说明您的问题? - SoroushA
1
我希望DialogFragment能够包裹其内容,以便在“提交”按钮处结束。 - Sam Stern
@hatboysam 最好你把整个DialogFragment的XML贴出来,这样才能更好地解决问题。我的一段代码中有一个DialogFragment,对于“wrap_content”可以正常工作(“尊重”)。 - Nitin Patel
@SamStern,你找到了一个合适的解决方案吗? - Gaurav Sarma
2个回答

12

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

<!-- 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>

第一个 View(一个间隔)正在扩展以填充视口,更改为以下内容即可修复:

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

我还需要将以下代码添加到 DialogFragmentonResume 方法中:

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

4

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

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

如果您需要在片段中包装内容,则可以遍历视图并总结总高度,然后将其用作布局高度。

我知道我可以手动设置,问题是如何使其实际换行。 - Sam Stern
1
@hotboysam 我在第二部分回答了你的问题。你需要找到高度并将它们相加。对话框的高度在布局之前确定,这就是为什么你不能只使用XML标记来包装它的原因。 - SoroushA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接