1
votes

Puis-je mettre un bouton en bas d'un ConstraintLayout dans ScrollView?

J'ai un ScrollView contenant un ConstraintLayout . Dans le ConstraintLayout , j'ai mis de nombreuses vues avec un attribut layout_constraintTop_toBottomOf pour faire une relation entre la vue et la vue de dessus, et j'ai utilisé layout_marginTop code > pour mettre des espaces entre les vues.

Dans ma conception, j'ai un bouton qui devrait être en bas de la mise en page et cela ne peut pas arriver avec le layout_marginTop car il devrait avoir une relation avec le en bas du ConstraintLayout.

Voici mon code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">

        <TextView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="120dp"
            android:text="Logo"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/un_et"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="28dp"
            android:layout_marginTop="25dp"
            android:layout_marginRight="28dp"
            android:gravity="center"
            android:hint="User name"
            android:textColor="#bebebe"
            android:textCursorDrawable="@null"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/logo" />


        <EditText
            android:id="@+id/pw_et"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="28dp"
            android:layout_marginTop="13dp"
            android:layout_marginRight="28dp"
            android:gravity="center"
            android:hint="Password"
            android:inputType="textPassword"
            android:textColor="#bebebe"
            android:textCursorDrawable="@null"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/un_et" />

        <RelativeLayout
            android:id="@+id/save_pw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:paddingLeft="28dp"
            android:paddingRight="28dp"
            app:layout_constraintTop_toBottomOf="@id/pw_et">

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:buttonTint="#bebebe"
                android:text="Save account"
                android:textColor="#bebebe" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:text="Forget password?"
                android:textColor="#a40000" />
        </RelativeLayout>


        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="28dp"
            android:layout_marginTop="17dp"
            android:layout_marginRight="28dp"
            android:text="Login"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            app:layout_constraintTop_toBottomOf="@id/save_pw" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Sign up"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            app:layout_constraintTop_toBottomOf="@id/btn" />
    </android.support.constraint.ConstraintLayout>
</ScrollView>


8 commentaires

Ma question vous a-t-elle aidé? Si c'est le cas, mettez en évidence que c'est comme la question.


Les vues de ConstraintLayout sont-elles dynamiques? Je veux dire, le nombre de vues peut-il changer au fil du temps lorsque l'application est utilisée? Si tel est le cas, vous devez vérifier RecyclerView car il est conçu pour cette situation.


@ Code-Apprentice Exactement, ce que je me demandais car cela impliquerait d'utiliser RecyclerView


@ Code-Apprentice ce n'est pas dynamique, c'est juste une activité de connexion


@GauravMall ouais, merci


Ensuite, sélectionnez-le comme réponse!


@AbdurrahmanAnas Si la réponse de Gaurav ne résout toujours pas votre problème, modifiez votre question pour inclure un exemple minimal reproductible .


@ Code-Apprentice terminé, vérifiez-le


3 Réponses :


3
votes

Définissez simplement cette propriété sur votre bouton:

android:layout_marginBottom="height_of_button"

Ajoutez ensuite ceci à votre dernière vue cette propriété:

app:layout_constraintBottom_toTopOf="@+id/button"

Ajoutez également à votre dernière vue:

app:layout_constraintBottom_toBottomOf="parent"

Cependant, si vous ajoutez beaucoup de vues, il serait préférable de les remplir dans un RecyclerView à l'aide d'un adaptateur . Décidez de ce que vous voulez faire. Dites-moi aussi si j'ai bien compris votre question, c'était un peu déroutant.


5 commentaires

pourquoi avez-vous utilisé "width_of_button"? c'est = "match_parent" dans mon projet


C'est juste un espace réservé. Vous pouvez le changer en ce que vous voulez.


Dans votre cas match_parent


il y a une erreur lorsque j'essaye de faire ceci: [android: layout_marginBottom = "match_parent"], vous pouvez vérifier mon code ci-dessus je l'ai ajouté


Le bas de la marge ne doit pas correspondre au parent. Cela devrait être la hauteur de votre bouton. Je suis désolé de l'avoir étiqueté comme largeur



4
votes

Remplacez ScrollView par NestedScrollView et ajoutez également android: fillViewport = "true" comme ceci

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"> 

            // rest of code

            <Button
                android:id="@+id/btn2"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Sign up"
                android:textAllCaps="false"
                android:textColor="#FFFFFF"
                app:layout_constraintVertical_bias="1"
                app:layout_constraintTop_toBottomOf="@id/btn" 
                app:layout_constraintBottom_toBottomOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

p>


1 commentaires

Vous pouvez utiliser fillViewport sur un ScrollView normal.



1
votes

J'ai trouvé une solution pour garder le bouton en bas, peu importe que votre texte soit dynamique, lisez sur app: layout_constraintVertical_bias https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#Bias

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        android:text="Title"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintTop_toBottomOf="@id/text_gallery"
        app:layout_constraintBottom_toBottomOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:isScrollContainer="true">

            <androidx.appcompat.widget.AppCompatTextView
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/texto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text=" Text \n Text" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Buton1"
                app:layout_constraintVertical_bias="1"
                app:layout_constraintTop_toBottomOf="@+id/texto"
                app:layout_constraintBottom_toBottomOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>


0 commentaires