2012-08-10 10 views
12

Tengo un host de pestañas en el que tengo más de 6 botones. Sin embargo, la pantalla muestra solo 3-4 botones.Agregar desplazamiento en tabhost en android

A continuación se muestra el código que estoy usando junto con la imagen.

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

La pregunta es, ¿Cómo se supone que debo añadir un desplazamiento de modo que pueda desplazarse hasta los 6 botones o cualquier otro truco.

Saludos

Respuesta

23

Sólo nido del TabWidget dentro de un HorizontalScrollingView, así:

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

Por cierto, esta solución funciona para muchos otros escenarios, RadioGroup, por ejemplo

+0

que no trabaja para esto, no necesito grupo de radio: d –

+0

Sí, lo hice mal. El TabWidget necesita estar anidado ... Edité mi respuesta. Pruébalo ahora –

+0

¿También es necesario anidar el HorizontalScrollView dentro de dos RelativeLayouts? Mi anidación es similar a: Relativo> TabHost> Lineal> Horizontal> TabWidget y no se desplaza. ¿Crees que tal vez la API ha cambiado desde 2012? – James

Cuestiones relacionadas