Fragment's content above Tab's text in TabLayout

时间:2016-04-04 16:38:24

标签: android android-tablayout

I'm using TabLayout and I attach two tabs.

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

Then onTabSelected() I display the fragment I want according to which tab user selected.

The layout of my Activity is :

<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>

and the layout of the fragments is just a TextView in a LinearLayout :

<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>

But the result I get is the following :

enter image description here

as you can see the content of the fragment overlaps Tab's text when it should appear underneath. Thanks

1 个答案:

答案 0 :(得分:2)

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

您可以在每个Tab片段的布局上添加顶部填充。

例如,

机器人:?paddingTop =&#34; ATTR / actionBarSize&#34;

例如,我的第一个标签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>

现在,屏幕如下所示: enter image description here