相同高度的视图对于顶视图和底视图看起来不同

Same elevation of views looks different for top and bottom views

最近,我被一个问题惊呆了。在我的 android phone 中,具有相同高度的列表或滚动视图的元素具有与其定位相关的不同阴影。例如,屏幕顶部的视图有一个小的光影,每当屏幕底部的视图有更暗和更强的阴影.这是 Google Keep 应用程序的屏幕截图:

所以,我认为这是因为 Google 保持应用程序。也许 Google 的人决定在他们的应用程序中用阴影来做这个技巧。因此,我创建了一个示例应用程序。

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

    <TextView
        style="@style/AppButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Test"/>

</LinearLayout>

我的风格:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppButton">
        <item name="android:background">@color/colorAccent</item>
        <item name="android:elevation">4dp</item>
        <item name="android:gravity">center</item>
        <item name="android:padding">16dp</item>
        <item name="android:textColor">@android:color/white</item>
    </style>

</resources>

输出为:

如你所见,我们得到了相同的效果(顶视图阴影小,底视图阴影大)。所以我的问题是:

  1. 我做错了什么?
  2. 如何为所有视图实现相同的阴影(位置不同)?
  3. 此外,我没有在文档中找到任何解释,所以我可以在哪里阅读有关该现象的信息?
  4. 它是在所有设备上发生还是仅在特定设备上发生?

P.S.: 我有 Nexus 5X, Android 7.1.2.

更新: 重要的是,Android Studio Preview Window 一切都很好。每个视图都有与其他视图相同的阴影。但在真实设备上,您可以看到差异。

由高程 API 生成的阴影在 3D 中定位 space,这意味着每个阴影的外观不仅受其高程值的影响,还受阴影投射器的 x 和 y 位置的影响在屏幕上。这与现实世界非常相似 - 光源下的物体投射的阴影比远处的物体投射的阴影短。

仔细看看您发布的第一张图片。左侧卡片的左边缘和右侧卡片的右边缘的阴影比水平中心的边缘长。

广告。 1. 没有。它就是这样工作的。

广告。 2. 无法使用高程API。可以自己画阴影。

广告。 3. 不知道,抱歉。

广告。 4. 所有设备。

广告。更新。编辑器中的阴影是静态的,因此您观察不到任何效果。