12

我有一个标签主机,其中有 6 个以上的按钮。然而,屏幕仅显示 3-4 个按钮。

以下是我与图像一起使用的代码。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#777777">

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

        >
       <RelativeLayout
            android:id="@+id/layTab"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:background="@drawable/navbar_background"
            android:layout_alignParentBottom="true"
            android:layout_centerVertical="true"
            >
            <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            />
        </RelativeLayout>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/layTab"/>


   </RelativeLayout>
</TabHost>

在此处输入图像描述

问题是,我想如何添加一个滚动,以便我可以滚动到所有 6 个按钮或任何其他技巧。

最好的祝福

4

1 回答 1

25

只需将 TabWidget 嵌套在 Horizo​​ntalScrollingView 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#777777" >

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

        <RelativeLayout
            android:id="@+id/layTab"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerVertical="true"
            android:background="@drawable/navbar_background"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <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" >
                </TabWidget>
            </HorizontalScrollView>
        </RelativeLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/layTab"
            android:layout_alignParentTop="true" />
    </RelativeLayout>

</TabHost>

顺便说一句,此解决方案适用于许多其他场景,例如 RadioGroup

于 2012-08-10T15:33:40.617 回答