最近,我被一个问题惊呆了。在我的 android 手机上,具有相同高度的列表或 ScrollView 的元素具有与其定位相关的不同阴影。例如,屏幕顶部的 View 有一个小的光影,每当屏幕底部的 View 有更暗和更强的阴影。以下是 Google Keep 应用程序的屏幕截图:
所以,我认为这是因为 Google Keep 应用程序。也许谷歌的人决定在他们的应用程序中使用阴影来做这个技巧。因此,我创建了一个示例应用程序。
我的布局:
<?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>
输出是:
如你所见,我们有相同的效果(顶 View 有一个小阴影,底 View 有一个大阴影)。所以我的问题是:
- 我做错了什么?
- 如何为所有 View 实现相同的阴影(位置相同)?
- 此外,我没有在文档中找到任何解释,所以我可以在哪里阅读有关该现象的信息?
- 它是在所有设备上发生还是仅在特定设备上发生?
附注:我有 Nexus 5X,Android 7.1.2。
更新: 重要的一点是,从 Android Studio 预览窗口一切都很好。每个 View 都有与其他 View 相同的阴影。但在真实设备上,您可以看到差异。
最佳答案
Elevation API 生成的阴影位于 3D 空间中,这意味着每个阴影的外观不仅受其高程值的影响,还受阴影转换器在屏幕上的 x 和 y 位置的影响。这与现实世界非常相似 - 光源下的物体转换的阴影比距离较远的物体转换的阴影短。
仔细查看您发布的第一张图片。左侧卡片的左侧边缘和右侧卡片的右侧边缘的阴影比水平中心边缘的阴影更长。
广告。 1. 没有。它就是这样工作的。
广告。 2. 无法使用Elevation API。你可以自己画阴影。
广告。 3. 不知道,抱歉。
广告。 4. 所有设备。
广告。更新。编辑器中的阴影是静态的,因此您观察不到任何效果。
关于android - 相同高度的 View 对于顶 View 和底 View 看起来不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45035475/