2009-09-09 9 views
44

J'utilise un TableLayout. J'ai besoin d'avoir un défilement horizontal et vertical pour cette mise en page. Par défaut, je suis en mesure d'obtenir un défilement vertical dans la vue, mais le défilement horizontal ne fonctionne pas. J'utilise Android SDK 1.5 r3. J'ai déjà essayé android:scrollbars="horizontal".Comment faire pour que ma mise en page défile horizontalement et verticalement?

J'ai lu sur certains forums que dans la mise à jour de cupcake, le défilement horizontal est possible.

Comment faire défiler ma mise en page dans les deux directions?

Répondre

112

J'ai été capable de trouver un moyen simple d'atteindre les deux comportements de défilement.

Voici le code XML pour elle:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:scrollbars="vertical"> 

    <HorizontalScrollView 
     android:layout_width="320px" android:layout_height="fill_parent"> 

     <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/linlay" android:layout_width="320px" 
      android:layout_height="fill_parent" android:stretchColumns="1" 
      android:background="#000000"/> 

    </HorizontalScrollView> 

</ScrollView> 
+0

Le défilement horizontal fonctionne bien ... mais il n'y a pas de barres de défilement horizontales visibles. Comment pouvons-nous faire cela? – Amit

+4

avertissement: cela peut être une mauvaise pratique. Voir cette question/réponse: http://stackoverflow.com/questions/4490821/scrollview-inside-scrollview#4492050 –

+4

@PedroLoureiro en fait, la question dans le lien que vous avez posté se réfère à la combinaison de 'ScrollView' avec' ListView'. C'est parce que 'ListView' a une tonne d'optimisations qui ne jouent pas bien avec un' ScorollView' environnant. La réponse affichée ici n'utilise pas 'ListView', donc l'argument lié n'est pas pertinent. –

0

Dans ce post Scrollview vertical and horizontal in android ils parlent d'une solution possible, citant:

Matt Clark a construit une vue personnalisée en fonction de la source Android, et semble fonctionner parfaitement: http://blog.gorges.us/2010/06/android-two-dimensional-scrollview

Attention, la classe de cette page a un bug qui calcule la largeur de la vue. Un correctif par Manuel Hilty est dans les commentaires:

Solution: Remplacez l'instruction sur la ligne 808 par ce qui suit:

final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED); 
0

Utilisez ceci:

android:scrollbarAlwaysDrawHorizontalTrack="true" 

Exemple:

<Gallery android:id="@+id/gallery" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbarAlwaysDrawHorizontalTrack="true" /> 
1

il est trop tard, mais j'espère que votre problème sera résolu rapidement avec ce code. rien à faire plus juste mettre votre code ci-dessous scrollview.

<HorizontalScrollView 
      android:id="@+id/scrollView" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent"> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
       //xml code 
      </ScrollView> 
    </HorizontalScrollView> 
Questions connexes