如何使的Andorid的动作条/ TabWidget的LinearLayout来包装它的孩子们它的、孩子们、动作、Andorid

2023-09-12 03:25:37 作者:都一样

我尝试实施滚动标签,每个标签都有它的大小调整到它的内容的大小。

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

我尝试了两种方法:动作条和TabHost。在这两种情况下,我能够调整标签的大小。

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

要做到这一点我在每个选项卡setLayoutProperites后给予选项卡中添加。在这两种情况下相同的方法。的重量改变为0和宽度被设定为WRAP_CONTENT。下面以TabHost相关的例子。

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

我留下了相关的LinearLayout(LL)的大小问题。 LL的尺寸更大,它的所有儿童的尺寸:

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 :

标签的大小与预期一致:图片-1

Tab sizes are as expected: Image-1

LL标记,其大小是大(顶部),TabView的标记 - 它的大小是正确的(底部)的图片-2

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

布局XML为TabHost情况:

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>

推荐答案

我想它。它为某人也许有用...一个有呼叫的LinearLayout有标签setMeasureWithLargestChildEnabled(假);像这样:

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

tabHost.getTabWidget().setMeasureWithLargestChildEnabled(false);