问题描述
我无法在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
内的CardView
与layout_gravity="center"
({{1}扩展CardView
)