Android - how to make a scrollable constraintlayout?

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

    Take out bottom button from the nestedscrollview and take linearlayout as parent. Add bottom and nestedscrollview as thier children. It will work absolutely fine. In manifest for the activity use this - this will raise the button when the keyboard is opened

    android:windowSoftInputMode="adjustResize|stateVisible"
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical">
    
        <androidx.core.widget.NestedScrollView xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fillViewport="true">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/input_city_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="32dp"
                    android:layout_marginEnd="20dp"
                    android:hint="@string/city_name"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintTop_toTopOf="parent">
    
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/city_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                        android:lines="1"
                        android:maxLength="100"
                        android:textSize="16sp" />
    
                </com.google.android.material.textfield.TextInputLayout>
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </androidx.core.widget.NestedScrollView>
    
        <Button
            android:id="@+id/submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:onClick="onSubmit"
            android:padding="12dp"
            android:text="@string/string_continue"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="parent" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-30 20:33

    Please use below solution it has taken my lots of time to fix.

    Enjoy your time :)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:background="@color/white"
        >
    
        <ScrollView
            android:id="@+id/mainScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:fillViewport="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
    
                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    >
    
                </android.support.constraint.ConstraintLayout>
    
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>

    Use Exactly like this u will definitely find your solution...

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

    in scrollview make height and width 0 add Top_toBottomOfand Bottom_toTopOf constraints that's it.

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

    A lot of answers here, nothing really simple. It's important that the ScrollView's lauout_height is set to match_parent while the layout_height of the ContraintLayout is wrap_content

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
        ...
    
    
    0 讨论(0)
  • 2020-11-30 20:38

    For completing the previous answers I am adding the following example, which also takes into account the use of the AppBar. With this code, the Android Studio design editor seems to work fine with the ConstraintLayout.

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout
        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:fitsSystemWindows="true"
        android:background="@drawable/bg"
        android:orientation="vertical">
    
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.ActionBar.AppOverlayTheme">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    </android.support.design.widget.AppBarLayout>
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/image_id"
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_bar_height"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/intro"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />
    
            <TextView
                android:id="@+id/desc_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/intro_desc"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/image_id" />
    
            <Button
                android:id="@+id/button_scan"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:backgroundTint="@color/colorAccent"
                android:padding="8dp"
                android:text="@string/intro_button_scan"
                android:textStyle="bold"
                app:layout_constraintTop_toBottomOf="@+id/desc_id" />
    
            <Button
                android:id="@+id/button_return"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:backgroundTint="@color/colorAccent"
                android:padding="8dp"
                android:text="@string/intro_button_return"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/button_recycle" />
    
            <Button
                android:id="@+id/button_recycle"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:backgroundTint="@color/colorAccent"
                android:padding="8dp"
                android:text="@string/intro_button_recycle"
                android:textStyle="bold"
                app:layout_constraintTop_toBottomOf="@+id/button_scan" />
        </android.support.constraint.ConstraintLayout>
    </ScrollView>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题