Android:可滚动标签页

5
我目前正在开发我的第一个Android应用程序。我在我的应用程序中使用了选项卡布局。我按照开发指南上的教程进行了操作,但遇到了一个问题。该教程只使用了三个选项卡,但我需要更多。因此,选项卡会调整大小并挤在一起。我希望有人告诉我如何使它们像Dolphin浏览器在使用多个选项卡时一样滚动。谢谢!
3个回答

12

根据Yoel所说,只需添加以下内容: android:fillViewport="true" 以及 android:scrollbars="none"

将其添加到HorizontalScrollView中,就像这样:

    <HorizontalScrollView 
       android:id="@+id/horizontalScrollView1" 
       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="fill_parent"
           android:layout_height="wrap_content" />

    </HorizontalScrollView>

3
我建议将TabWidget与HotizontalScrollView一起使用,这样你仍然可以像原来一样使用选项卡。
    <HorizontalScrollView 
        android:id="@+id/horizontalScrollView1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    </HorizontalScrollView>

需要注意的是,如果您使用较少的标签,这将导致选项卡未填满屏幕的整个宽度。我目前正在寻找解决此问题的方法。


1
在这种情况下,您可能不想使用选项卡。相反,您可以将所有“选项卡”放入一个水平线性布局中,位于屏幕顶部,并用ScrollView包装,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent">
    <LinearLayout 
        android:orientation="horizontal" android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        ...buttons go here...

    </LinearLayout>
</HorizontalScrollView>

我认为这将给你想要的结果,但是让我知道


已编辑为使用HorizontalScrollView而不是ScrollView(仅垂直滚动) - mtmurdock
谢谢。我会尝试以这种方式实现并告诉你。 - ariets

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接