Home > Enterprise >  How can put my title at top of the ImageView
How can put my title at top of the ImageView

Time:11-13

I want to put my Title at top of the ImageView. However, I can not use layout_above to put my title at the top. 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: enter image description here

Actual:

enter image description here

Expected:

enter image description here

CodePudding user response:

You don't need to wrap ImageView and TextView in another RelativeLayout. It's actually easier and more performant to put them both directly in 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>

enter image description here

  • Related