gpt4 book ai didi

android - View 显示在另一个 View 下方,该 View 在 Android 中具有较低的海拔

转载 作者:行者123 更新时间:2023-12-05 00:06:50 24 4
gpt4 key购买 nike

我遇到了一个看似奇怪的问题,当一个 View 包含在父 View 组中时,该 View 显示在另一个海拔较低的 View 下方。下面是我能做的最简单示例的代码 fragment 。 View (正方形)具有完全相同的高度。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="@android:color/white">


<!-- Elevation working properly -->
<View
android:id="@+id/dark_overlay_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0.3"
android:background="@android:color/black"
android:elevation="7dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


<!-- Elevation working properly -->
<View
android:id="@+id/view_displayed_above_overlay"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#C8F8C8"
android:elevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


<!-- Here's the trouble -->
<android.support.constraint.ConstraintLayout
android:id="@+id/troublesome_parent"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<View
android:id="@+id/view_that_wont_display_above_overlay"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#C8F8C8"
android:elevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

显示如下图:

Image showing android screen with 2 green squares with the same elevation. However, one of the squares are displayed below a with lower elevation.

我已经尝试更改所显示 View 的 Root View 和父 View 的布局类型,没关系。

我知道高度是从 View 的直接父级继承的,但是如果一个 View 具有更高的高度,它不应该显示在另一个 View 之上吗?

我还在 Android Studio 的 Layout Inspector 中比较过这两个方 block 。唯一的区别是 x 位置(和 ID)。

为简化起见,我的目标是:

  • 没有高程的 ViewGroup。
  • 该 ViewGroup 的 subview ,高度为 8dp
  • View (ViewGroup 的同级 View )在 subview 下方显示了 7dp 高度。

最佳答案

您只需将分配的高度切换为根布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="@android:color/white">


<!-- Elevation working properly -->
<View
android:id="@+id/dark_overlay_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:alpha="0.3"
android:background="@android:color/black"
android:elevation="7dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


<!-- Elevation working properly -->
<View
android:id="@+id/view_displayed_above_overlay"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#C8F8C8"
android:elevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>


<!-- Here's the trouble -->
<android.support.constraint.ConstraintLayout
android:id="@+id/troublesome_parent"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/white"
android:elevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<View
android:id="@+id/view_that_wont_display_above_overlay"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#C8F8C8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

关于android - View 显示在另一个 View 下方,该 View 在 Android 中具有较低的海拔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50474185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com