android - 在 ConstraintLayout 中垂直居中 2 个视图
问题描述
我有 2 个视图需要垂直居中(两者)。我需要在 2 个视图之间固定上边距 (40 dp) 如何使用 ConstraintLayout 来做到这一点?
谢谢。
解决方案
您需要使用链,在这种情况下app:layout_constraintVertical_chainStyle="packed"
将实现您正在寻找的东西。
尝试这样的事情:
<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_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1"
app:layout_constraintBottom_toTopOf="@+id/text_view_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="TextView 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_view_1" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- mongodb - Mongodb - 成功更新后将文档属性的总和设置为另一个
- docker - 可以将只读分区中的文件作为卷安装在 docker 容器中吗?
- amazon-web-services - aws pinpoint中是否有用于通过邮递员获取自定义事件数据的api?
- python - 嵌套 JSON,字符串索引必须是整数
- c# - 如何在 Web Api 中为 JSON Post 正确配置方法?
- c# - 事件处理程序仅在 Microsoft Office VSTO 加载项中触发一次
- amazon-ec2 - Kubernetes 主节点未就绪状态
- python - 如何让 Python 在没有硬编码路径名的情况下在子文件夹中查找文件?
- java - 如何检索我的数据库值并显示在我的微调器中供用户选择?
- sql-server - 使用 TRY - CATCH 设置 ARITHABORT 和 ANSI_WARINGS ON