TabLayout 中 Fragment 的内容高于 Tab 的文本

Fragment's content above Tab's text in TabLayout

我正在使用 TabLayout 并附加了两个选项卡。

    tabLayout.addTab(tabLayout.newTab().setText("Featured"));
    tabLayout.addTab(tabLayout.newTab().setText("Filter"));

然后onTabSelected()我根据用户选择的标签显示我想要的片段。

我的 Activity 的布局是:

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/ll_container" 
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

    <FrameLayout 
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

片段的布局只是 LinearLayout 中的 TextView :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView 
        android:text="Projects List Fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

但我得到的结果如下:

如您所见,片段的内容在 Tab 的文本应显示在下方时与 Tab 的文本重叠。 谢谢

最后,我找到了解决这个问题的替代方案。

您可以在每个 Tab 片段的布局上添加顶部内边距。

例如,

android:paddingTop="?attr/actionBarSize"

例如,我的第一个标签 xml 如下所示:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.upgautam.uddhav.productassignment.uicontrollers.ProductListFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/product_list_string" />

</android.support.design.widget.CoordinatorLayout>

现在,屏幕如下所示: