将layout_height设置为wrap_content的片段

kuh*_*hnp 7 android android-fragments

我正在使用包含多个片段的NestedScrollView.我希望每个片段占据他们需要的高度,而不必硬编码每个片段的大小.理想情况下,我想将片段(或其容器)的layout_height设置为wrap_content,但这不起作用.片段已实例化但未出现.

<me.henrytao.smoothappbarlayout.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:name="com.example.MyFragment1"/>

        <fragment
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:name="com.example.MyFragment2"/>

        <fragment
            android:id="@+id/fragment3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:name="com.example.MyFragment3"/>

    </LinearLayout>


</me.henrytao.smoothappbarlayout.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

它尝试了两种方法:在布局文件中声明Fragment; 或者将FrameLayout作为容器并使用Fragment Manager从Activity添加片段,但它们都不起作用.