问题描述
我正在努力使工具栏阴影出现在Android 28+上.我尝试了多种建议的解决方案,但没有一个可行.以下样式适用于28以下的所有设备:
I'm struggling to make toolbar shadow appear on Android 28+. I tried multiple solutions suggested, but none worked. Following styles work well for all devices below 28:
toolbar.xml
toolbar.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
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)
styles.xml (v21)
<style name="Widget.App.Toolbar" parent="Widget.App.Toolbar.V1">
<item name="android:elevation">4dp</item>
</style>
我还尝试使用ViewCompat.setElevation
以编程方式设置高程,但没有成功.
I also tried to set elevation programmatically, using ViewCompat.setElevation
with no success.
有人知道如何使其工作吗?
Does anyone has an idea how to make it work?
UPD:我知道您可以创建自定义阴影可绘制对象,但是如果可能的话,我想避免这种解决方案.只是不明白为什么这样的基本功能需要额外的可绘制对象和布局视图.另外,我想知道为什么它可以在以前的Android版本上运行.找不到任何证据证明这种行为已改变.
UPD:I know you can create custom shadow drawable, but I would like to avoid this solution if possible. Just can't understand why such basic functionality requires additional drawable and views in layout. Also, I wonder why it works on previous Android versions. Didn't found any proofs that this behavior has changed.
推荐答案
我不知道为什么它对许多人无效,
I don't know why it doesn't work for many of the people, adding:
android:elevation="5dp"
直接进入工具栏对我来说很好.
Directly to the toolbar works fine for me.
这篇关于Android 9上没有工具栏高程阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!