如何显示 android 中的元素

How to display over an element in android

我正在按照教程制作井字游戏,我希望当用户赢得游戏时,我的图像会弹出并显示在所有其他背景图像之上。弹出图像并显示游戏获胜,图像显示在所有其他元素之上。相反,元素显示在图像上。

enter image description here

<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:context=".MainActivity">
    
// Popup windows for displaying the image
    <LinearLayout
        android:id="@+id/showwinner"
        android:visibility="invisible"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="153dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/tropy_istock" />
            </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/winners"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textAllCaps="true"
                android:textSize="30sp"
                android:layout_gravity="center_horizontal|center"
                />

            <Button
                android:id="@+id/newgameButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="newGame"
                android:text="PlayAgain"
                android:layout_gravity="center_horizontal|center"
                />
            </LinearLayout>
    </LinearLayout>
// End of pop up window

    <androidx.gridlayout.widget.GridLayout
        android:id="@+id/mainbox"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="1dp"
        android:background="@drawable/tttbackground"
        app:columnCount="3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="3">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box8"
            android:layout_width="101dp"
            android:layout_height="70dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="30dp"
            android:onClick="dropIn"
            android:tag="8"
            app:layout_column="2"
            app:layout_row="2" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box7"
            android:layout_width="101dp"
            android:layout_height="70dp"

            android:layout_marginTop="30dp"
            android:onClick="dropIn"
            android:tag="7"
            app:layout_column="1"
            app:layout_row="2" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box6"
            android:layout_width="101dp"
            android:layout_height="70dp"

            android:layout_marginTop="30dp"
            android:onClick="dropIn"
            android:tag="6"
            app:layout_column="0"
            app:layout_row="2" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box5"
            android:layout_width="101dp"
            android:layout_height="70dp"
            android:layout_marginLeft="10dp"

            android:layout_marginTop="40dp"
            android:onClick="dropIn"
            android:tag="5"
            app:layout_column="2"
            app:layout_row="1" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box4"
            android:layout_width="101dp"
            android:layout_height="70dp"

            android:layout_marginTop="40dp"
            android:onClick="dropIn"
            android:tag="4"
            app:layout_column="1"
            app:layout_row="1" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box3"
            android:layout_width="101dp"
            android:layout_height="70dp"

            android:layout_marginTop="40dp"
            android:onClick="dropIn"
            android:tag="3"
            app:layout_column="0"
            app:layout_row="1" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box2"
            android:layout_width="101dp"
            android:layout_height="70dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="30dp"
            android:onClick="dropIn"
            android:tag="2"
            app:layout_column="2"
            app:layout_row="0" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box1"
            android:layout_width="101dp"
            android:layout_height="70dp"

            android:layout_marginTop="30dp"
            android:onClick="dropIn"
            android:tag="1"
            app:layout_column="1"
            app:layout_row="0" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/box"
            android:layout_width="101dp"
            android:layout_height="70dp"

            android:layout_marginTop="40dp"
            android:onClick="dropIn"
            android:tag="0"
            app:layout_column="0"
            app:layout_row="0" />

    </androidx.gridlayout.widget.GridLayout>

有两种方法可以做到这一点

1) 保持奖杯布局为 ConstraintLayout

的最后一个 child
   <?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:context=".MainActivity">
    
    
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#a00">
            <!--Your Views-->
        </GridLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF018786"
            android:gravity="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="80dp"
                android:layout_height="80dp"
                app:srcCompat="@drawable/trophy_istock" />
        </RelativeLayout>
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>

2)如果你想把它放在其他地方,请给elevation:

   <?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:context=".MainActivity">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF018786"
            android:gravity="center"
            android:elevation="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="80dp"
                android:layout_height="80dp"
                app:srcCompat="@drawable/trophy_istock" />
        </RelativeLayout>
    
        
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#a00">
            <!--Your Views-->
        </GridLayout>
    
       
    
    </androidx.constraintlayout.widget.ConstraintLayout>