translationZ 在我的带有地图片段的约束布局中不起作用



我有一个地图上的图像视图,它们都放在约束布局中。我为 ImageView 设置 android:translationZ="2dp",然后 android:elevation="2dp",但仍然看不到图像。

map_main.xml:

<?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">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MapsActivity" />
<ImageView
android:id="@+id/testImage"
android:layout_width="64dp"
android:layout_height="64dp"
app:layout_constraintBottom_toBottomOf="@+id/mapView"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/test"
android:translationZ="2dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

奇怪的是,我将OnClickListener设置为图像,我可以单击它。看起来 ImageView 就在那里,在地图片段的顶部,但我看不到它。

如何将地图片段置于底层或将视图置于顶层?

android:src更改app:srcCompat工作正常。最终的 xml 如下所示:

<?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">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MapsActivity" />
<ImageView
android:id="@+id/testImage"
android:layout_width="64dp"
android:layout_height="64dp"
app:layout_constraintBottom_toBottomOf="@+id/mapView"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/test"
android:translationZ="2dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

感谢@mohosyny和@ReazMurshed。

最新更新