如何在CardView上方设置按钮? 正在使用XML,但现在App启动时

问题描述

我无法在CardView上方放置按钮。
当我为按钮设置translationZ时,它在XML中显示在上方,但是当我启动该应用程序时,该按钮仍未显示在上方。

这是我的XML:

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

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:cardCornerRadius="10dp"
        app:cardElevation="12dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/imageDrawable"/>

    </androidx.cardview.widget.CardView>

    <Button
        android:id="@+id/button"
        android:layout_width="38dp"
        android:layout_height="40dp"
        android:elevation="16dp"
        android:translationZ="16dp"
        android:background="@drawable/ic_play"
        app:layout_constraintBottom_toBottomOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="@+id/cardView"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="@+id/cardView" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是一个多次询问的问题,但我看不出任何无法正常运行的原因。

它只是ImageView上方的播放按钮。

我还更改了按钮在XML中的位置,将其置于最后位置,没有任何改变。

解决方法

考虑将Button内的CardViewlayout_gravity="center"({{1}扩展CardView

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...