Android 如何将按钮同时约束到屏幕底部和文本视图

Android 如何将按钮同时约束到屏幕底部和文本视图,android,android-constraintlayout,Android,Android Constraintlayout,我有以下简单的应用程序设计。工具栏、文本视图和按钮 如何构建此布局设计,以便: 当文本很小时,按钮被限制在屏幕底部 当文本较长时,它不会延伸到按钮下方,而是延伸滚动视图的高度 编辑:以下是@MariosP的解决方案。将按钮约束到文本视图和屏幕底部。但是,如果您这样做,您将看到按钮默认为平均两个约束,因此它在两个垂直约束之间平均浮动,这不是我想要的。诀窍是在按钮上使用app:layout\u constraintVertical\u bias=“1”。这告诉布局,您不希望按钮处于垂直约束的中间,

我有以下简单的应用程序设计。工具栏、文本视图和按钮

如何构建此布局设计,以便:

  • 当文本很小时,按钮被限制在屏幕底部
  • 当文本较长时,它不会延伸到按钮下方,而是延伸滚动视图的高度

  • 编辑:以下是@MariosP的解决方案。将按钮约束到文本视图和屏幕底部。但是,如果您这样做,您将看到按钮默认为平均两个约束,因此它在两个垂直约束之间平均浮动,这不是我想要的。诀窍是在按钮上使用app:layout\u constraintVertical\u bias=“1”。这告诉布局,您不希望按钮处于垂直约束的中间,而是希望位于两个约束的最远底部(底部边缘)。在0和1之间更改偏移数将使按钮沿这些垂直约束滑动。如果您的布局中按钮悬停在其他文本视图上,则此信息将帮助您解决此问题。

    您可以使用带有ConstraintLayout子项的滚动视图来实现此行为。以下是xml布局:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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:background="@android:color/holo_blue_dark"
        android:fillViewport="true">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:id="@+id/button"
                android:layout_width="200dp"
                android:layout_height="80dp"
                android:text="Button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:backgroundTint="@android:color/holo_red_light" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
                android:textColor="@android:color/white"
                android:textSize="16sp"
                android:layout_margin="40dp"
                app:layout_constraintVertical_bias="0"
                app:layout_constraintBottom_toTopOf="@+id/button"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar" />
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:background="@android:color/holo_green_light"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </ScrollView>
    
    
    

    结果为长文本(滚动到底部后):


    您可以分享您当前的布局吗?或者你做过的任何事