Android - how to make a scrollable constraintlayout?

前端 未结 17 1489
故里飘歌
故里飘歌 2020-11-30 19:46

I want to make a layout that lets me scroll down using constraint layout, but I don\'t know how to go about it. Should the ScrollView be the parent of the

相关标签:
17条回答
  • 2020-11-30 20:14

    To summarize, you basically wrap your android.support.constraint.ConstraintLayout view in a ScrollView within the text of the *.xml file associated with your layout.

    Example activity_sign_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SignInActivity"> <!-- usually the name of the Java file associated with this activity -->
    
        <android.support.constraint.ConstraintLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/gradient"
            tools:context="app.android.SignInActivity">
    
            <!-- all the layout details of your page -->
    
        </android.support.constraint.ConstraintLayout>
    </ScrollView>
    

    Note 1: The scroll bars only appear if a wrap is needed in any way, including the keyboard popping up.

    Note 2: It also wouldn't be a bad idea to make sure your ConstraintLayout is big enough to the reach the bottom and sides of any given screen, especially if you have a background, as this will ensure that there isn't odd whitespace. You can do this with spaces if nothing else.

    0 讨论(0)
  • 2020-11-30 20:14

    Constraintlayout is the Default for a new app. I am "learning to Android" now and had a very hard time figuring out how to handle the default "sample" code to scroll when a keyboard is up. I have seen many apps where I have to close the keyboard to click "submit" button and sometimes it does not goes away. Using this [ScrollView / ContraintLayout / Fields] hierarchy it is working just fine now. This way we can have the benefits and ease of use from ConstraintLayout in a scrollable view.

    0 讨论(0)
  • 2020-11-30 20:15

    Just use constraint layout inside NestedScrollView or ScrollView.

    <android.support.v4.widget.NestedScrollView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white">
    
     </android.support.constraint.ConstraintLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    thats it. enjoy your coding.

    0 讨论(0)
  • 2020-11-30 20:16

    This is how I resolved it:
    If you are using Nested ScrollView i.e. ScrollView within a ConstraintLayout then use the following configuration for the ScrollView instead of "WRAP_CONTENT" or "MATCH_PARENT":


    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/someOtherWidget"
        app:layout_constraintTop_toTopOf="parent">
    
    0 讨论(0)
  • 2020-11-30 20:18

    There is a bug in version 2.2 that makes it impossible to scroll the ConstraintLayout. I guess it still exists. You can use LinearLayout or RelativeLayout alternatively.

    Also, check out: Is it possible to put a constraint layout inside a ScrollView.

    0 讨论(0)
  • 2020-11-30 20:20

    Use NestedScrollView with viewport true is working good for me

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="700dp">
    
            </android.support.constraint.ConstraintLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    for android x use this

     <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">
    .....other views....
    
    </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.core.widget.NestedScrollView>
    
    0 讨论(0)
提交回复
热议问题