You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中ConstraintLayout内的ScrollView无法正常工作求助

问题:ScrollView在ConstraintLayout中无法正常滚动的原因及解决办法

我使用ConstraintLayout布局,包含头部栏(header)和底部按钮,想要将ScrollView放置在头部栏与底部按钮之间。我了解ScrollView只能包含一个子元素,因此在ScrollView内嵌套了LinearLayout,并将ScrollView的高度设置为0dp,使其填充头部与底部之间的空间,但ScrollView无法正常工作。请问问题出在哪里?请帮忙解决!

原布局代码

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rule_header"
        android:paddingVertical="20dp"
        app:layout_constraintBottom_toTopOf="@+id/registerScrollView"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/goBackBtn"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_arrow_circle_gray" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="회원가입"
            android:textSize="18dp"
            android:textStyle="bold" />

    </RelativeLayout>

    <ScrollView
        android:id="@+id/registerScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/registerBtn"
        app:layout_constraintTop_toBottomOf="@id/header">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingHorizontal="50dp"
            android:paddingVertical="20dp">


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                android:paddingHorizontal="20dp"
                android:paddingVertical="10dp">

                <ImageView
                    android:id="@+id/avatarImage"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:background="@drawable/avatar_background"
                    android:clipToOutline="true"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_user" />

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/avatarButton"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:text="사진 선택"
                    android:textSize="18dp">

                </androidx.appcompat.widget.AppCompatButton>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/nameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/nameLabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="50dp"
                    android:layout_weight="1"
                    android:text="이름"
                    android:textSize="20dp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@id/nameInput"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <EditText
                    android:id="@+id/nameInput"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:inputType="text"
                    android:paddingBottom="15dp"
                    android:textSize="20dp"
                    android:theme="@style/EditTheme"
                    tools:text="신혜정" />

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/registerBtn"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/default_button"
        android:text="회원가입 완료"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/registerScrollView" />

</androidx.constraintlayout.widget.ConstraintLayout>

问题分析与修复

导致ScrollView无法正常工作的两个核心问题:

  1. 无效的layout_weight属性:nameLayout是垂直方向LinearLayout的子元素,设置android:layout_weight="1"完全没有作用,反而会打乱ScrollView内部布局的高度计算,限制了可滚动空间。
  2. 错误的约束属性:nameLabel的父布局是LinearLayout,却使用了app:layout_constraintXXX这类ConstraintLayout专属的约束属性,这些属性不会被解析,属于无效代码,会导致布局逻辑异常。

修复后的布局代码

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rule_header"
        android:paddingVertical="20dp"
        app:layout_constraintBottom_toTopOf="@+id/registerScrollView"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/goBackBtn"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_arrow_circle_gray" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="회원가입"
            android:textSize="18dp"
            android:textStyle="bold" />

    </RelativeLayout>

    <ScrollView
        android:id="@+id/registerScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/registerBtn"
        app:layout_constraintTop_toBottomOf="@id/header">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingHorizontal="50dp"
            android:paddingVertical="20dp">


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                android:paddingHorizontal="20dp"
                android:paddingVertical="10dp">

                <ImageView
                    android:id="@+id/avatarImage"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:background="@drawable/avatar_background"
                    android:clipToOutline="true"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_user" />

                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/avatarButton"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:text="사진 선택"
                    android:textSize="18dp">

                </androidx.appcompat.widget.AppCompatButton>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/nameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/nameLabel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="50dp"
                    android:layout_weight="1"
                    android:text="이름"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/nameInput"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:inputType="text"
                    android:paddingBottom="15dp"
                    android:textSize="20dp"
                    android:theme="@style/EditTheme"
                    tools:text="신혜정" />

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/registerBtn"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/default_button"
        android:text="회원가입 완료"
        android:textColor="@color/white"
        android:textSize="20dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/registerScrollView" />

</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者Hyejung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:31:58