【问题标题】:Adding scroll in tabhost in android在android的tabhost中添加滚动
【发布时间】:2012-08-10 15:28:51
【问题描述】:

我有一个标签主机,其中有超过 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 个按钮或任何其他技巧。

最好的问候

【问题讨论】:

    标签: java android eclipse android-layout


    【解决方案1】:

    只需将 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

    【讨论】:

    • 是的,我做错了。 TabWidget 需要嵌套......我编辑了我的答案。立即尝试
    • Horizo​​ntalScrollView是不是也需要嵌套在两个RelativeLayout里面?我的嵌套如下:Relative>TabHost>Linear>Horizo​​ntal>TabWidget 并且它不滚动。您认为自 2012 年以来 API 是否发生了变化?
    • 为我工作,使用 TabHost - LinearLayout。
    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多