简体   繁体   English

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

[英]Can I put a Button to bottom of a ConstraintLayout inside ScrollView?

I have a ScrollView contains a ConstraintLayout .我有一个ScrollView包含一个ConstraintLayout Inside the ConstraintLayout , I put many views with an attribute layout_constraintTop_toBottomOf to make a relation between the view and the view top of it, and I used layout_marginTop to put spaces between the views.ConstraintLayout内部,我放置了许多带有属性layout_constraintTop_toBottomOf的视图,以在视图和视图顶部之间建立关系,并使用layout_marginTop在视图之间放置空格。

In my design, I have a Button that should be in the bottom of the layout and it cannot happen with the layout_marginTop because it should have a relation with the bottom of the ConstraintLayout .在我的设计中,我有一个 Button 应该在布局的底部,它不能与layout_marginTop发生,因为它应该与ConstraintLayout的底部有关系。

Here's my code:这是我的代码:

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

Replace ScrollView with NestedScrollView & also add android:fillViewport="true" like thisNestedScrollView替换ScrollView并像这样添加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>

Just set this property to your button:只需将此属性设置为您的按钮:

app:layout_constraintBottom_toBottomOf="parent"

Then add this to your last view this property:然后将此添加到您上次查看此属性中:

app:layout_constraintBottom_toTopOf="@+id/button"

Also, add to your last view:此外,添加到您的最后一个视图:

android:layout_marginBottom="height_of_button"

However, if you are adding a lot of views it would be better to populate them inside a RecyclerView using an Adapter.但是,如果您要添加大量视图,最好使用 Adapter 将它们填充到RecyclerView 中 Decide what you want to do.决定你想做什么。 Also, tell me if I understood your question correctly, it was a bit confusing.另外,告诉我我是否正确理解了您的问题,这有点令人困惑。

I found one solution to keep the button at the bottom, doesn't matter is your text is dynamic, read about app:layout_constraintVertical_bias https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#Bias我找到了一种将按钮保持在底部的解决方案,不管你的文本是动态的,阅读关于app:layout_constraintVertical_bias https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#Bias

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        android:text="Title"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintTop_toBottomOf="@id/text_gallery"
        app:layout_constraintBottom_toBottomOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:isScrollContainer="true">

            <androidx.appcompat.widget.AppCompatTextView
                app:layout_constraintTop_toTopOf="parent"
                android:id="@+id/texto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text=" Text \n Text" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Buton1"
                app:layout_constraintVertical_bias="1"
                app:layout_constraintTop_toBottomOf="@+id/texto"
                app:layout_constraintBottom_toBottomOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM