ConstraintLayout中元素elevation值相同时的层级显示规则咨询
当两个View的elevation值相同时的层级规则
当多个View的elevation值相同时,它们的视觉层级由添加到父布局的顺序决定:后被添加(在布局文件中后声明)的View会被绘制在更早添加的View之上。这个行为是Android的预期规则,在所有设备上表现一致,并非随机结果。
以你提供的布局代码为例:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/view1" android:layout_width="200dp" android:layout_height="200dp" android:background="@android:color/red" android:elevation="8dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <View android:id="@+id/view2" android:layout_width="200dp" android:layout_height="200dp" android:background="@android:color/blue" android:elevation="8dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
view1先在布局中声明,view2后声明,所以view2会被绘制在view1的上层,最终显示蓝色,这完全符合Android的布局绘制逻辑。
内容的提问来源于stack exchange,提问作者pequeñaluna
相关产品推荐
相关产品推荐

