我可以将 Button 放在 ScrollView 中 ConstraintLayout 的底部吗?

Abd*_*nas 4 android scrollview android-constraintlayout

我有一个ScrollView包含一个ConstraintLayout. 在 中ConstraintLayout,我放置了许多带有属性layout_constraintTop_toBottomOf的视图,以在视图和视图顶部之间建立关系,并且我曾经layout_marginTop在视图之间放置空格。

在我的设计中,我有一个 Button 应该在布局的底部,它不会发生在 ,layout_marginTop因为它应该与ConstraintLayout.

这是我的代码:

<ScrollView 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.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">

        <TextView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="120dp"
            android:text="Logo"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/un_et"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="28dp"
            android:layout_marginTop="25dp"
            android:layout_marginRight="28dp"
            android:gravity="center"
            android:hint="User name"
            android:textColor="#bebebe"
            android:textCursorDrawable="@null"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/logo" />


        <EditText
            android:id="@+id/pw_et"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="28dp"
            android:layout_marginTop="13dp"
            android:layout_marginRight="28dp"
            android:gravity="center"
            android:hint="Password"
            android:inputType="textPassword"
            android:textColor="#bebebe"
            android:textCursorDrawable="@null"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/un_et" />

        <RelativeLayout
            android:id="@+id/save_pw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:paddingLeft="28dp"
            android:paddingRight="28dp"
            app:layout_constraintTop_toBottomOf="@id/pw_et">

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:buttonTint="#bebebe"
                android:text="Save account"
                android:textColor="#bebebe" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:text="Forget password?"
                android:textColor="#a40000" />
        </RelativeLayout>


        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="28dp"
            android:layout_marginTop="17dp"
            android:layout_marginRight="28dp"
            android:text="Login"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            app:layout_constraintTop_toBottomOf="@id/save_pw" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Sign up"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            app:layout_constraintTop_toBottomOf="@id/btn" />
    </android.support.constraint.ConstraintLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

Gau*_*all 5

只需将此属性设置为您的按钮即可:

app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)

然后将此属性添加到您上次查看的内容中:

app:layout_constraintBottom_toTopOf="@+id/button"
Run Code Online (Sandbox Code Playgroud)

另外,添加到您的最后一个视图:

android:layout_marginBottom="height_of_button"
Run Code Online (Sandbox Code Playgroud)

但是,如果您要添加大量视图,最好使用适配器将它们填充到RecyclerView中。决定你想做什么。另外,请告诉我我是否正确理解了你的问题,这有点令人困惑。


Jig*_*ani 5

替换ScrollViewNestedScrollView& 也android:fillViewport="true"像这样添加

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

            // rest of code

            <Button
                android:id="@+id/btn2"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="Sign up"
                android:textAllCaps="false"
                android:textColor="#FFFFFF"
                app:layout_constraintVertical_bias="1"
                app:layout_constraintTop_toBottomOf="@id/btn" 
                app:layout_constraintBottom_toBottomOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

  • 您可以在常规“ScrollView”上使用“fillViewport”。 (2认同)