TabLayout 位于 Framelayout 的内容之下

TabLayout goes under the content of Framelayout

我有一个 FrameLayout,其中包含一个 TabLayout 和一个 ViewPager。显然 ViewPager 加载了一个片段。现在的问题是 TabLayout 在加载时位于 fragment 的内容之下,因此消失了。

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

            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        </FrameLayout>

我该如何解决?

编辑:

使用以下代码我能够让它工作,但现在滚动时选项卡总是卡在顶部...无论如何要解决这个问题?

<LinearLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
</LinearLayout>

更改为垂直 LinearLayout。然后 ViewPager 将出现在 TabLayout 下方,而不是覆盖它。

您应该使用方向为 'vertical' 的 LinearLayout 而不是 FrameLayout。当您想要包含单个子项(例如片段)时,通常会使用 FrameLayout。

FrameLayout 实际上将其所有子级堆叠在一起。因此,在您的情况下,这不是理想的行为。您应该考虑使用垂直 LinearLayoutRelativeLayout.

<LinearLayout
     android:id="@+id/content_frame"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1"
     android:orientation="vertical" >

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

</LinearLayout>