web-dev-qa-db-fra.com

Comment faites-vous un LinearLayout scrollable?

Il y a beaucoup d'éléments à l'écran et je dois utiliser la barre de défilement pour permettre à l'utilisateur de faire défiler l'écran. Cependant, le parchemin n'est pas visible ou ne fonctionne pas. Comment est-il possible d'ajouter une barre de défilement à un LinearLayout?

230
Troj

Enveloppez la disposition linéaire avec un <ScrollView>

Voir ici pour un exemple:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
       Android:layout_width="fill_parent"
       Android:layout_height="fill_parent"
       xmlns:Android="http://schemas.Android.com/apk/res/Android">
       <ScrollView
            Android:layout_width="fill_parent"
            Android:layout_height="wrap_content">
            <LinearLayout 
                  Android:layout_width="wrap_content"
                  Android:layout_height="wrap_content"
                  Android:orientation="vertical">
                  <!-- Content here -->
            </LinearLayout>
      </ScrollView>
 </LinearLayout>
440
Bryan Denny
<ScrollView 
      xmlns:Android="http://schemas.Android.com/apk/res/Android"
      Android:id="@+id/scroll" 
      Android:layout_width="match_parent"
      Android:layout_height="wrap_content">

      <LinearLayout 
            Android:id="@+id/container"
            Android:orientation="vertical" 
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content">
      </LinearLayout>

 </ScrollView>
139
krunal shah

Voici comment je l'ai fait par essais et erreurs.

ScrollView - (le wrapper externe).

 LinearLayout (child-1).

      LinearLayout (child-1a).

      LinearLayout (child-1b

Comme Scrollview ne peut avoir qu'un seul enfant, cet enfant est une disposition linéaire. Ensuite, tous les autres types de mise en page apparaissent dans la première mise en page linéaire. Je n'ai pas encore essayé d'inclure une mise en page relative, mais ils me rendent dingue alors j'attendrai jusqu'à ce que ma santé mentale revienne.

4
Patrick Murphy

Cela peut être fait en utilisant la balise <ScrollView>. Pour ScrollView, vous devez vous rappeler une chose à savoir ScrollView doit avoir un seul enfant.

Si vous voulez faire défiler votre mise en page complète, ajoutez <ScrollView> en haut. Vérifiez l'exemple donné ci-dessous.

<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:id="@+id/scroll" 
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <LinearLayout 
        Android:id="@+id/container" 
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:orientation="vertical">
            <!-- Content here -->
    </LinearLayout>

</ScrollView>

Mais si vous souhaitez faire défiler une partie de votre mise en page, ajoutez <ScrollView> au sein de cette partie. Vérifiez l'exemple donné ci-dessous.

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="400dp">

    <ScrollView
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">

        <LinearLayout 
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:orientation="vertical">
                <!-- Content here -->
        </LinearLayout>

    </ScrollView>

</LinearLayout>
3
Avijit Karmakar

vous devez utiliser l'attribut suivant et l'insérer dans la présentation linéaire

<LinearLayout ...>
<scrollView ...> 

</scrollView>
</LinearLayout>
1
Engineering Mind

Vous devez placer ScrollView en tant que premier enfant du fichier de mise en forme et placez maintenant votre représentation linéaire à l'intérieur. Maintenant, Android décidera, en fonction du contenu et de la taille de l'appareil disponible, d'afficher ou non un défilement.

Assurez-vous que linearlayout n'a pas de frère parce que ScrollView ne peut pas avoir plus d'un enfant.

0
Tulsi
 <LinearLayout 
        xmlns:Android="http://schemas.Android.com/apk/res/Android"
        xmlns:tools="http://schemas.Android.com/tools"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:orientation="vertical"
        tools:context=".MainActivity">

        <ScrollView
            Android:layout_width="match_parent"
            Android:layout_height="match_parent">

            <LinearLayout
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:orientation="vertical"`enter code here`>

                <---------Content Here --------------->
            </LinearLayout>
       </ScrollView>
    </LinearLayout>
0
anksanu