简体   繁体   English

如何将我的标题放在 ImageView 的顶部 - Android Studio

[英]How can put my title at top of the ImageView - Android Studio

I want to put my Title at top of the ImageView.我想把我的标题放在 ImageView 的顶部。 However, I can not use layout_above to put my title at the top.但是,我不能使用 layout_above 将我的标题放在顶部。 When I trying this my text is invisible.当我尝试这个时,我的文字是不可见的。 The title in the last image is exactly the alignment I want.最后一张图片中的标题正是我想要的对齐方式。 How can I do that ?我怎样才能做到这一点 ?

Code:代码: 在此处输入图片说明

Actual:实际的:

在此处输入图片说明

Expected:预期的:

在此处输入图片说明

You don't need to wrap ImageView and TextView in another RelativeLayout .您不需要将ImageViewTextView包装在另一个RelativeLayout It's actually easier and more performant to put them both directly in ConstraintLayout .将它们直接放在ConstraintLayout实际上更容易、更ConstraintLayout Then it's only a question of setting the constraints correctly.那么这只是正确设置约束的问题。 Demo code:演示代码:

<?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>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM