2011-01-27 80 views
2

我在写一个实现嵌套选项卡的应用程序。由于两组选项卡占用相当多的空间,并且通常因为内容的性质,我想将整个内部TabHost放入可滚动结构中。我可以使外部活动FrameLayout,LinearLayout,甚至ViewFlipper的tabcontent;当我尝试使它成为ScrollView时,程序崩溃。Android - TabHost里面滚动查看

<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喜欢住在不可滚动的框架内。有没有什么办法可以解决这个问题而不会造成一大堆混乱?

回答

3

对不起,我自己想清楚了。解决方案是将第二个TabHost包装在自己的XML文件中的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" /> 
     <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>