3

enter image description here

As you can see the fragment content "New Text" begins at the same line as the tab widget. The tab content is simply:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

and tha tab layout:

<android.support.v4.app.FragmentTabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabhost">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"/>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"/>

        </FrameLayout>
    </RelativeLayout>
</android.support.v4.app.FragmentTabHost >

I tried with padding but padding is not very secure because the tab widget height changes on orientation change. I also tried

android:layout_alignParentBottom="@android:id/tabs"

on the frame layout, however this has no effect.

How can I position the tab content correctly under the tab widget/navigation?

EDIT fragment setup:

mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabhost);

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"),
                SomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"),
                SomeFragment.class, null);
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • I don't see anything wrong here. Could you please post some code and the complete XML layout of your fragment? – Dalmas Jul 13 '14 at 18:38

1 Answers1

2

EDIT

Your problem is in FragmentTabHost setup method. Please see the example at the top + description of setup(Context context, FragmentManager manager, int containerId) method.
http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html


First of all android:layout_alignParentBottom doesn't use id as a value, but boolean (true/false) - it tells whether child should or should not align its bottom bound with parent bottom bound. You can set it to true, but only this will not resolve your problem. The attribute you're looking for is android:layout_below="@android:id/tabs". Using these two attributes means the tabcontent view will be right below the TabWidget (id: @android:id/tabs) and will occupy the whole rest of screen up to the parent bottom bound.

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@android:id/tabs"
        android:layout_alignParentBottom="true"
        >

BTW. Please use "match_parent" instead of "fill_parent" - it means the same but "fill_parent" is an old naming and it was deprecated in Froyo (API level 8). More info here: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT
and here:
Is deprecated word the only difference between fill_parent and match_parent


If you have problems with RelativeLayout please just use LinearLayout instead:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabhost">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"/>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

        </FrameLayout>
    </LinearLayout>
</android.support.v4.app.FragmentTabHost>
Community
  • 1
  • 1
Maciej Ciemięga
  • 10,125
  • 1
  • 41
  • 48
  • I just tried this (I've been having the exact same problem) and it didn't work - the tabs and framelayout still overlap. – u3l Jul 13 '14 at 18:12
  • I can confirm this does not have any effect. – DarkLeafyGreen Jul 13 '14 at 18:19
  • strange... I've added LinearLayout solution that will work for sure. – Maciej Ciemięga Jul 13 '14 at 18:21
  • Same result on my end unfortunately. (The `LinearLayout` was actually what I tried first). – u3l Jul 13 '14 at 18:23
  • Can you check the hierarchyview? Please check where the fragments are attached. Imo it is not possible to overlap anything if the fragment is attached anywhere inside @android:id/tabcontent. Can you post the more code? For example FragmentTabHost setup? – Maciej Ciemięga Jul 13 '14 at 18:32
  • "mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabhost);" the last argument is supposed to be a tabcontent ... http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html#setup(android.content.Context, android.support.v4.app.FragmentManager, int) – Maciej Ciemięga Jul 13 '14 at 18:38
  • You're welcome:) I've also added this to the top of my answer. – Maciej Ciemięga Jul 13 '14 at 18:41