【问题标题】:DialogFragment will not respect wrap_contentDialogFragment 将不尊重 wrap_content
【发布时间】:2017-08-25 01:01:31
【问题描述】:

我有一个如下所示的自定义 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"

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

【问题讨论】:

  • 每个组件都有一个最小高度。它看起来对我来说是正确的。你能澄清你的问题吗?
  • 我希望 DialogFragment 包装它的内容,所以它会在“提交”按钮处结束。
  • @hatboysam 最好把 dialogFragment 的整个 xml 贴出来来解决问题。我的一个代码有 DialogFragment 可以正常工作('尊重')wrap_content
  • @SamStern 你找到合适的解决方案了吗?

标签: android android-layout android-appcompat


【解决方案1】:

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

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

我还必须在DialogFragment 中将此添加到onResume

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

【讨论】:

    【解决方案2】:

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

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

    如果需要将内容包裹在fragment中,可以遍历view,求和总高度,然后作为布局高度。

    【讨论】:

    • 我知道我可以手动设置,问题是如何让它真正换行。
    • @hotboysam 我在第二部分回答了你的问题。你需要找到高度并总结它们。对话框的高度是在布局之前确定的,这就是为什么你不能只使用 xml 标签来包装它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2011-04-30
    • 2021-04-15
    • 2014-03-08
    相关资源
    最近更新 更多