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

如何构建这样的布局设计:
的高度。
编辑:下面介绍了@MariosP的解决方案。将按钮限制到文本视图和屏幕底部。但是,如果您这样做,您将看到该按钮默认为两个约束的平均值,因此它垂直地在这两个约束之间浮动,这不是我想要的。诀窍是在按钮上使用app:layout_constraintVertical_bias="1"。这告诉布局,您不希望按钮位于它的垂直约束的中间,而是希望位于两个约束的最底层(底部边缘)。改变0到1之间的偏置数将使按钮沿着这些垂直约束滑动。如果您得到的布局按钮是悬停在您的其他文本视图,这一信息将帮助您解决。
发布于 2021-04-02 14:04:13
您可以使用ScrollView和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>一个小文本的结果:

结果有一个长文本(在滚动到底部之后):

https://stackoverflow.com/questions/66893973
复制相似问题