Search code examples
androidandroid-linearlayoutandroid-tabhostandroid-framelayouttabwidget

android:layout_marginBottom not leaving space at bottom


I have a TabHost activity in my project. When I have the following XML, the tabs are off the screen and they don't show at all.

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activityVerticalMargin"
    android:paddingLeft="@dimen/activityHorizontalMargin"
    android:paddingRight="@dimen/activityHorizontalMargin"
    android:paddingTop="@dimen/activityVerticalMargin"
    tools:context=".SomeActivity">

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="50dp">

            <some elements here>

        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dp" />

    </LinearLayout>

</TabHost>

However, if I slightly modify to look like this, the tabs show but they are not at the bottom and it is not how I want it to look.

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activityVerticalMargin"
    android:paddingLeft="@dimen/activityHorizontalMargin"
    android:paddingRight="@dimen/activityHorizontalMargin"
    android:paddingTop="@dimen/activityVerticalMargin"
    tools:context=".SomeActivity">

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="50dp">

            <!-- layout_height above was changed to wrap_content -->

            <some elements here>

        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dp" />

    </LinearLayout>

</TabHost>

Why is it not working in the first case? Isn't layout_marginBottom supposed to make the height of the layout match_parent - margin (which is 50dp in this case)?


Solution

  • Convert parent LinearLayout to Relativelayout. Frame layout should have parameter align_parenttop = true and the tabwidget should have align_parentBottom = true. Let the rest remain.