Android DialogFragment 没有 WRAP_CONTENT

Android DialogFragment doesn't WRAP_CONTENT

单击 TextView 时会显示 DialogFragment。这个 DialogFragment 有自己的 XML 布局文件,底部有一个 ListView 和一个 EditTextButton

问题是当ListView只有几个项目时,DialogFragment仍然占据了大部分屏幕(即高度比应该的大很多。

这里是我的意思的截图(黑色的是ListView有一项):

如您所见,当我希望 Dialog 只是它需要的大小时,ListView 上方有大量空白 space。

这里是 DialogFragent XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/spacing_small"
    tools:context="com.cohenadair.anglerslog.fragments.ManageFragment">

    <LinearLayout
        android:id="@+id/add_item_layout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/new_item_edit"
            android:layout_weight="1"
            android:hint="@string/hint_new_item"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_add"
            android:id="@+id/add_button"
            android:layout_margin="0dp"/>

    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/add_item_layout"
        android:id="@+id/content_list_view"
        android:background="@android:color/black">

    </ListView>

</RelativeLayout>

onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_manage_primitive, container, false);

    int primitiveId = getArguments().getInt(ARG_PRIMITIVE_ID);
    PrimitiveFragmentInfo info = FragmentUtils.primitiveInfo(getActivity(), primitiveId);

    if (info != null) {
        initViews(view, info);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    return view;
}

谢谢!

我通过从 RelativeLayout 切换到 LinearLayoutlayout_weight 属性.

解决了这个问题

这是新的 XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/spacing_small"
    android:orientation="vertical"
    tools:context="com.cohenadair.anglerslog.fragments.ManageFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/content_list_view">

    </ListView>

    <LinearLayout
        android:id="@+id/add_item_layout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/new_item_edit"
            android:layout_weight="1"
            android:hint="@string/hint_new_item"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_add"
            android:id="@+id/add_button"/>

    </LinearLayout>

</LinearLayout>