Android的 - TabHost内滚动型Android、TabHost

2023-09-06 05:47:30 作者:墨尔本゛因你而晴

我正在写一个实现嵌套标签的应用程序。由于两套标签占据相当多的空间,一般是因为内容的本质,我想整个内TabHost放入一个可滚动的结构。我可以做外活动的FrameLayout,LinearLayout中,甚至ViewFlipper的tabcontent;当我试图让它滚动型,程序崩溃。

I'm writing an app that implements nested tabs. Because two sets of tabs occupy quite a bit of space and generally because the nature of the content I would like to place the whole inner TabHost into a scrollable structure. I can make tabcontent of the outer activity FrameLayout, LinearLayout, even ViewFlipper; when I try to make it ScrollView, the program crashes.

<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喜欢住非滚动框架内。有没有什么办法去解决这个而无需创建一大堆乱七八糟的?

So apparently TabHost likes to live inside non-scrollable frames. Is there any way to go around this without creating a whole lot of a mess?

推荐答案

对不起,我想通了我自己。解决的办法是其自己的XML文件中包装一个滚动型内的第二TabHost。这工作就好了。

Sorry I figured it out on my own. The solution is to wrap the second TabHost inside a ScrollView within its own XML file. That works just fine.

外:

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