【发布时间】:2019-05-07 13:28:13
【问题描述】:
我正在努力让工具栏阴影出现在 Android 28+ 上。我尝试了多种建议的解决方案,但没有一个奏效。以下样式适用于 28 岁以下的所有设备:
工具栏.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
style="@style/Widget.App.Toolbar"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_height="?attr/actionBarSize"
/>
styles.xml
<style name="Widget.App.Toolbar.V1" parent="Widget.AppCompat.Toolbar">
<item name="android:layout_height">?attr/actionBarSize</item>
<item name="android:layout_width">match_parent</item>
<item name="android:background">?attr/colorPrimary</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="android:theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
styles.xml (v21)
<style name="Widget.App.Toolbar" parent="Widget.App.Toolbar.V1">
<item name="android:elevation">4dp</item>
</style>
我还尝试使用ViewCompat.setElevation 以编程方式设置海拔,但没有成功。
有人知道如何让它工作吗?
更新: 我知道您可以创建自定义阴影可绘制对象,但如果可能,我想避免使用此解决方案。只是无法理解为什么这样的基本功能需要额外的可绘制和布局视图。另外,我想知道为什么它可以在以前的 Android 版本上运行。没有找到任何证据表明这种行为已经改变。
【问题讨论】: