如何将我的标题放在 ImageView 的顶部
How can put my title at top of the ImageView
我想将我的标题放在 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>
我想将我的标题放在 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>