我正在努力使工具栏阴影出现在安卓28+上。我尝试了多种解决方案,但都没有效果。以下样式适用于28岁以下的所有设备:
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
<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,但没有成功。
有谁知道怎么让它工作吗?
UPD:我知道您可以创建自定义阴影绘图,但是如果可能的话,我想避免使用这个解决方案。只是不明白为什么这样的基本功能在布局中需要额外的可绘制和视图。另外,我想知道为什么它能在以前的Android版本上工作。没有找到任何证据证明这种行为已经改变。
发布于 2019-05-07 13:42:24
最后,我为工具栏设置了自己的下拉阴影,认为它可能对任何寻找它的人都有帮助:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_alizarin"
android:titleTextAppearance="@color/White"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- **** Place Your Content Here **** -->
<View android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/toolbar_dropshadow"/>
</FrameLayout>
</LinearLayout>@drawable/工具栏_下拉阴影:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="@android:color/transparent"
android:endColor="#88333333"
android:angle="90"/>
</shape>@颜色/颜色茜素
<color name="color_alizarin">#e74c3c</color>

发布于 2019-05-07 13:54:28
我不知道为什么它对很多人不起作用,补充道:
android:elevation="5dp"直接到工具栏对我来说很好。
https://stackoverflow.com/questions/56023753
复制相似问题