android - 如何让 Andorid 的 ActionBar/TabWidget LinearLayout 包裹它的 child

标签 android android-layout android-actionbar android-linearlayout android-tabhost

我尝试实现可滚动选项卡,其中每个选项卡的大小都根据其内容大小进行了调整。

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

为了实现这一点,我在添加给定选项卡后在每个选项卡上设置了 LayoutProperites。在这两种情况下,方法相同。权重更改为 0,宽度设置为 WRAP_CONTENT。下面是 TabHost 相关的例子。

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 的大小大于其所有子项的大小:

标签大小符合预期:Image-1

LL 已标记且其大小过大(顶部),TabView 已标记 - 大小正确(底部):Image-2

TabHost 案例的布局 xml:

<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(false);。像这样:

tabHost.getTabWidget().setMeasureWithLargestChildEnabled(false);

关于android - 如何让 Andorid 的 ActionBar/TabWidget LinearLayout 包裹它的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024127/

相关文章:

android - 是否可以以编程方式更改操作栏选项卡指示器

android - R.java- 在 android 中处理重复的 View ID

android - Android 中的日期验证

Android:基于屏幕尺寸的不同图像

android:隐藏另一个布局后布局背景变黑

java - 登录尝试持续时间

android - onPrepareOptionsMenu 重复 ActionBar 中的项目

Android webrtc 不适用于 3g、4g

android-layout - 如何在不可见的文本字段中输入文本

Android 操作栏填充选项卡宽度