Android - how to make a scrollable constraintlayout?

前端 未结 17 1491
故里飘歌
故里飘歌 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:21

    There is a type of constraint which breaks the scroll function:

    Just make sure you are not using this constraint on any view when wanting your ConstraintLayout to be scrollable with ScrollView :

    app:layout_constraintBottom_toBottomOf=“parent”
    

    If you remove these your scroll should work.

    Explanation:

    Setting the height of the child to match that of a ScrollView parent is contradictory to what the component is meant to do. What we want most of the time is for some dynamic sized content to be scrollable when it is larger than a screen/frame; matching the height with the parent ScrollView would force all the content to be displayed into a fixed frame (the height of the parent) hence invalidating any scrolling functionality.

    This also happens when regular direct child components are set to layout_height="match_parent".

    If you want the child of the ScrollView to match the height of the parent when there is not enough content, simply set android:fillViewport to true for the ScrollView.

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

    For me, none of the suggestions about removing bottom constraints nor setting scroll container to true seemed to work. What worked: expand the height of individual/nested views in my layout so they "spanned" beyond the parent by using the "Expand Vertically" option of the Constraint Layout Editor as shown below.

    For any approach, it is important that the dotted preview lines extend vertically beyond the parent's top or bottom dimensions

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

    you need surrounded my constraint-layout with a ScrollView tag and gave it the property android:isScrollContainer="true".

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

    It seems that it is working, I don't know what dependency you were working with but in this one

    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    

    Is working, this is what I did

    <?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=".MainActivity">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.design.widget.TextInputLayout
                android:id="@+id/til_input"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="Escriba el contenido del archivo"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/btn_save"
                app:layout_constraintTop_toTopOf="@id/btn_save"
                app:layout_constraintVertical_chainStyle="spread">
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </android.support.design.widget.TextInputLayout>
    
            <Button
                android:id="@+id/btn_save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClickButtonSave"
                android:text="Guardar"
                app:layout_constraintLeft_toRightOf="@+id/til_input"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/txt_content"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/til_input"
                app:layout_constraintVertical_chainStyle="spread"
                app:layout_constraintVertical_weight="1" />
    
            <Button
                android:id="@+id/btn_delete"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:onClick="onClickButtonDelete"
                android:text="Eliminar"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/txt_content"
                app:layout_constraintVertical_chainStyle="spread" />
    
        </android.support.constraint.ConstraintLayout>
    
    </ScrollView>
    

    Scroll Top

    Scroll Bottom

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

    TO make a scrollable layout, the layout is correct. It will not be scrollable until there is reason to scroll(just like in any other layout). So add enough content and it will be scrollable, just like with any layout(Linear, Relative, etc). However, you cannot scroll properly in Blueprint or design-mode when designing with ConstraintLayout and ScrollView.

    Meaning:

    You can make a scrollable ConstraintLayout, but it will not scroll properly in the editor due to a bug/scenario that wasn't considered. But even though scrolling doesn't work in the editor, it works on devices. (I have made several scrolling COnstraintLayouts, so I have tested it)

    Note

    Regarding your code. The ScrollView is missing a closing tag, I don't know if it is the case in the file or if it is a copy-paste miss, but you may want to look at it.

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

    You can use HorizontalScrollView and it'll work as well!

    0 讨论(0)
提交回复
热议问题