2012-08-10 59 views
12

我有一個標籤主機,其中我有超過6個按鈕。但是屏幕只顯示3-4個按鈕。在android中的tabhost添加滾動

以下是我與圖像一起使用的代碼。

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

enter image description here

的問題是,我怎麼想添加一個滾動,這樣我可以滾動高達所有6個按鈕或任何其他伎倆。

問候

回答

23

就巢Horizo​​ntalScrollingView內TabWidget,就像這樣:

<?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中,例如

+0

不工作這,我不需要廣播組:d – 2012-08-10 15:54:30

+0

是的,我做錯了。 TabWidget需要嵌套...我編輯了我的答案。立即試用 – 2012-08-10 15:59:27

+0

是否還需要在兩個RelativeLayouts中嵌套Horizo​​ntalScrollView?我的嵌套如下所示:相對> TabHost>線性>水平> TabWidget,它不滾動。你認爲自2012年以來API可能已經發生了變化嗎? – James 2016-10-04 13:23:26