How to make Andorid's ActionBar/TabWidget LinearLayout to wrap it's children
Asked Answered
P

1

1

I try to implement the scrollable tabs where each tab has it's size adjusted to it's content size.

I tried two approaches: ActionBar and TabHost. In both cases I was able to adjust tabs sizes.

To achieve that I setLayoutProperites on each Tab after given Tab was added. In both cases the same approach. The weight is changed to 0 and width is set to WRAP_CONTENT. Below the TabHost related example.

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams();
        params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        params.weight=0;
        tabHost.getTabWidget().getChildTabViewAt(i).setLayoutParams(params);

I'm left with the issue related to the size of the LinearLayout (LL). The size of LL is greater that the sizes of all its children :

Tab sizes are as expected: Image-1

LL is marked and its size is to big(top), TabView is marked - it's size is correct (bottom): Image-2

The layout xml for TabHost case:

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

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:orientation="vertical">

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />
        </HorizontalScrollView>

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

        <android.support.v4.view.ViewPager
            android:id="@+id/tabHost_viewpager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</TabHost>

Pelias answered 19/1, 2015 at 11:56 Comment(0)
P
2

I figured it. It maybe useful for somebody... One have call on LinearLayout having tabs setMeasureWithLargestChildEnabled(false);. Like so:

tabHost.getTabWidget().setMeasureWithLargestChildEnabled(false);
Pelias answered 29/1, 2015 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.