ScrollView作为ConstraintLayout的元素在Kotlin中无法正常工作。

huangapple go评论62阅读模式
英文:

ScrollView as element of ContraintLayout is not working in Kotlin

问题

以下是您要翻译的内容:

I have ConstraintLayout and it has header and bottom Button.
我有一个ConstraintLayout,它有标题和底部按钮。

I will put ScrollView in the middle of header and bottom.
我将把ScrollView放在标题和底部之间。

When I search I see that ScrollView should have only one element, I put LinearLayout in ScrollView.
当我搜索时,我发现ScrollView应该只有一个元素,我在ScrollView中放置了一个LinearLayout。

And ScrollView has height as 0dp since it should be extended between header and button.
ScrollView的高度设置为0dp,因为它应该在标题和按钮之间展开。

However, my ScrollView doesn't work.
然而,我的ScrollView不起作用。

What is the problem?
问题是什么?

Please help me!
请帮助我!

英文:

I have ConstraintLayout and it has header and bottom Button.

I will put ScrollView in the middle of header and bottom.

When I search I see that ScrollView should have only one element, I put LinearLayout in ScrollView.

And ScrollView has height as 0dp sine it should be extended between header and button.

However my ScrollView doesn't work.

What is the prolbem?

Please help me!

<?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>

答案1

得分: 0

以下是您提供的代码的翻译部分:

What exactly is your problem?
If I put more elements in the ScrollView, it works.

<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>

请注意,这是提供的 XML 代码的翻译版本。如果您需要任何进一步的帮助或解释,请随时提出。

英文:

What exactly is your problem?
If I put more elements in the ScrollView, it works.

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;RelativeLayout
android:id=&quot;@+id/header&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:background=&quot;@drawable/rule_header&quot;
android:paddingVertical=&quot;20dp&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/registerScrollView&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/goBackBtn&quot;
android:layout_width=&quot;30dp&quot;
android:layout_height=&quot;30dp&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_marginLeft=&quot;10dp&quot;
android:background=&quot;@drawable/ic_arrow_circle_gray&quot; /&gt;
&lt;TextView
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_centerVertical=&quot;true&quot;
android:text=&quot;회원가입&quot;
android:textSize=&quot;18dp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;ScrollView
android:id=&quot;@+id/registerScrollView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:fillViewport=&quot;true&quot;
android:scrollbars=&quot;vertical&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/registerBtn&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/header&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;50dp&quot;
android:paddingVertical=&quot;20dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;20dp&quot;
android:paddingVertical=&quot;10dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/avatarImage&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:background=&quot;@drawable/avatar_background&quot;
android:clipToOutline=&quot;true&quot;
android:scaleType=&quot;centerInside&quot;
android:src=&quot;@drawable/ic_user&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/avatarButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;40dp&quot;
android:text=&quot;사진 선택&quot;
android:textSize=&quot;18dp&quot;&gt;
&lt;/androidx.appcompat.widget.AppCompatButton&gt;
&lt;/LinearLayout&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;50dp&quot;
android:paddingVertical=&quot;20dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;20dp&quot;
android:paddingVertical=&quot;10dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/avatarImage&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:background=&quot;@drawable/avatar_background&quot;
android:clipToOutline=&quot;true&quot;
android:scaleType=&quot;centerInside&quot;
android:src=&quot;@drawable/ic_user&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/avatarButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;40dp&quot;
android:text=&quot;사진 선택&quot;
android:textSize=&quot;18dp&quot;&gt;
&lt;/androidx.appcompat.widget.AppCompatButton&gt;
&lt;/LinearLayout&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;50dp&quot;
android:paddingVertical=&quot;20dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;20dp&quot;
android:paddingVertical=&quot;10dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/avatarImage&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:background=&quot;@drawable/avatar_background&quot;
android:clipToOutline=&quot;true&quot;
android:scaleType=&quot;centerInside&quot;
android:src=&quot;@drawable/ic_user&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/avatarButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;40dp&quot;
android:text=&quot;사진 선택&quot;
android:textSize=&quot;18dp&quot;&gt;
&lt;/androidx.appcompat.widget.AppCompatButton&gt;
&lt;/LinearLayout&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;50dp&quot;
android:paddingVertical=&quot;20dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:paddingHorizontal=&quot;20dp&quot;
android:paddingVertical=&quot;10dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/avatarImage&quot;
android:layout_width=&quot;70dp&quot;
android:layout_height=&quot;70dp&quot;
android:background=&quot;@drawable/avatar_background&quot;
android:clipToOutline=&quot;true&quot;
android:scaleType=&quot;centerInside&quot;
android:src=&quot;@drawable/ic_user&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/avatarButton&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;40dp&quot;
android:text=&quot;사진 선택&quot;
android:textSize=&quot;18dp&quot;&gt;
&lt;/androidx.appcompat.widget.AppCompatButton&gt;
&lt;/LinearLayout&gt;
&lt;LinearLayout
android:id=&quot;@+id/nameLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:layout_weight=&quot;1&quot;
android:orientation=&quot;horizontal&quot;&gt;
&lt;TextView
android:id=&quot;@+id/nameLabel&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginRight=&quot;50dp&quot;
android:layout_weight=&quot;1&quot;
android:text=&quot;이름&quot;
android:textSize=&quot;20dp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toStartOf=&quot;@id/nameInput&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/nameInput&quot;
android:layout_width=&quot;150dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center&quot;
android:inputType=&quot;text&quot;
android:paddingBottom=&quot;15dp&quot;
android:textSize=&quot;20dp&quot;
android:theme=&quot;@style/EditTheme&quot;
tools:text=&quot;신혜정&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;
&lt;Button
android:id=&quot;@+id/registerBtn&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;60dp&quot;
android:background=&quot;@drawable/default_button&quot;
android:text=&quot;회원가입 완료&quot;
android:textColor=&quot;@color/white&quot;
android:textSize=&quot;20dp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/registerScrollView&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案2

得分: 0

有约束的问题。如果将滚动视图高度设置为包裹内容而不是0dp会发生什么。

英文:

There seems to problem of constraints. What happens if you set scrollview height to wrap content instead of 0dp.

答案3

得分: 0

如果我在主LinearLayout中添加一堆ImageView,就像这样:

<ScrollView>
<LinearLayout>
...
<ImageView/>
<ImageView/>
<ImageView/>
</LinearLayout>
</ScrollView>

我得到了这个(部分滚动)

ScrollView作为ConstraintLayout的元素在Kotlin中无法正常工作。

需要滚动的内容是什么?您是如何添加它的?实际上,一个ScrollView只是一个需要设置大小以充当UI中的“窗口”的FrameLayout(您已经通过约束完成了),然后您将内容放在其中。通常是另一个高度为wrap_content的布局,然后您在该布局中添加内容,其高度会增加。如果ScrollView小于其内容,那么它将滚动。

英文:

If I add a bunch of ImageViews inside your main LinearLayout, so it's like this:

&lt;ScrollView&gt;
&lt;LinearLayout&gt;
...
&lt;ImageView/&gt;
&lt;ImageView/&gt;
&lt;ImageView/&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;

I get this (partially scrolled)

ScrollView作为ConstraintLayout的元素在Kotlin中无法正常工作。

What are you adding that needs to be scrolled? How are you adding it? All a ScrollView really is is a FrameLayout that needs to be sized so it acts as a "window" in your UI (which you've done with the constraints), and then you put something inside it. Usually another layout with height wrap_content, and then you add stuff to that layout and its height grows. If the ScrollView is smaller than its contents, then it'll scroll.

huangapple
  • 本文由 发表于 2023年2月14日 00:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定