【发布时间】:2021-12-25 01:35:12
【问题描述】:
我想把我的标题放在ImageView 的顶部。但是,我不能使用layout_above 将我的标题放在顶部。当我尝试这个时,我的文字是不可见的。最后一张图片中的标题正是我想要的对齐方式。我该怎么做?
实际:
预期:
【问题讨论】:
-
欢迎来到 SO!请始终在此处直接包含图像/代码。我已经编辑了你的问题
我想把我的标题放在ImageView 的顶部。但是,我不能使用layout_above 将我的标题放在顶部。当我尝试这个时,我的文字是不可见的。最后一张图片中的标题正是我想要的对齐方式。我该怎么做?
实际:
预期:
【问题讨论】:
您无需将ImageView 和TextView 包装在另一个RelativeLayout 中。
将它们直接放在ConstraintLayout 中实际上更容易且性能更高。那么这只是正确设置约束的问题。演示代码:
<?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/above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title above"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="@id/image"
app:layout_constraintEnd_toEndOf="@id/image"/>
<ImageView
android:id="@+id/image"
android:layout_width="400px"
android:layout_height="400px"
android:src="@color/purple_200"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/above"/>
<TextView
android:id="@+id/overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text overlay"
app:layout_constraintStart_toStartOf="@id/image"
app:layout_constraintTop_toTopOf="@id/image"/>
</androidx.constraintlayout.widget.ConstraintLayout>
【讨论】: