安卓 - 在ScrollView中嵌套TabHost

3

我正在编写一个实现嵌套标签页的应用程序。因为两组标签占用了相当多的空间,通常情况下,由于我想要放置整个内部TabHost到可滚动结构中,所以我需要将其放入可滚动结构中。我可以将外部活动FrameLayout、LinearLayout甚至ViewFlipper的tabcontent设置好,但是当我尝试将其设置为ScrollView时,程序会崩溃。

<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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <ScrollView
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>

</TabHost>

显然,TabHost喜欢存在于不可滚动的框架内。有没有什么方法可以解决这个问题而不会制造很多麻烦?

1个回答

3
抱歉,我自己解决了。解决方法是将第二个TabHost包装在一个ScrollView中,放在它自己的XML文件中。这样做就可以正常工作了。
外部:
<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">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>

</TabHost>

内部:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TabHost 
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <ViewFlipper
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
        </LinearLayout>

    </TabHost>

</ScrollView>

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