简体   繁体   中英

How can I make an element appear on top of a layout

I know this question has been asked many times. However all the answers I have tried to find have failed me. I want the textview to appear on top of the constraint layout but every time I try to run the application I just see the web view vidged which is connected to the constraint layout.

The last solution I tried was to reduce elevation of the layout to 1dp and increase elevation of the TextView to 2dp.

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="@drawable/green_border_background" android:elevation="1dp">


<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="top"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          android:elevation="2dp"
          android:id="@+id/textView2"
          tools:text="0"
          android:textSize="30sp"

/>

At the moment I do not care if the textview is centered, vertically below or vertically above the layout. I just want it to be visible. It is under the layout (Z vise) and thereby invisible. Elevation is the problem I am facing.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <TextView
            android:textSize="25sp"
            android:layout_marginStart="50dp"
            android:text="Title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

This code place the TextView on the top of the ConstraintLayout

Just check the ConstraintLayout .

Read the "Adjust the constraint bias" part, try to set the

app:layout_constraintVertical_bias="0"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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