2012-08-10 11 views
12

Mam hosta zakładki, w którym mam więcej niż 6 przycisków. Jednak ekran pokazuje tylko 3-4 przyciski.Dodawanie przewijania w tabhost w Androidzie

Poniżej znajduje się kod, którego używam wraz z obrazem.

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

Pytanie brzmi, jak mam przypuszczać, aby dodać zwój tak, że mogę przewinąć do połowy wszystkich 6 przycisków lub jakiejkolwiek innej sztuczki.

Pozdrawiam

Odpowiedz

23

Wystarczy gniazdem TabWidget wewnątrz HorizontalScrollingView, tak:

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

Nawiasem mówiąc, to rozwiązanie działa w wielu innych sytuacjach, radiogroup np

+0

nie pracuje dla to, nie potrzebuję grupy radiowej: d –

+0

Tak, zrobiłem to źle. TabWidget musi zostać zagnieżdżony ... Edytowałem swoją odpowiedź. Wypróbuj teraz –

+0

Czy jest również konieczne zagnieżdżenie HorizontalScrollView wewnątrz dwóch RelativeLayouts? Moje zagnieżdżanie przebiega następująco: Względny> TabHost> Liniowy> Poziomo> TabWidget i nie przewija. Czy myślisz, że API zmieniło się od 2012 roku? – James

Powiązane problemy