【问题标题】:No toolbar elevation shadow on Android 9Android 9 上没有工具栏高度阴影
【发布时间】: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 版本上运行。没有找到任何证据表明这种行为已经改变。

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    我最终为工具栏设置了自己的阴影,认为它可能对寻找它的人有所帮助:

    <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/toolbar_dropshadow:

    <?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/color_alizarin

    <color name="color_alizarin">#e74c3c</color>
    

    【讨论】:

    • 感谢您的回复,但如果可能,我想避免使用此解决方案。只是无法理解为什么这样的基本功能需要额外的可绘制和布局视图。另外,我想知道为什么它可以在以前的 Android 版本上运行。没有找到任何证据表明这种行为已经改变。
    • 并非如此,它需要冗余的布局结构,不方便使用。更不用说性能损耗了。
    【解决方案2】:

    我不知道为什么它对许多人不起作用,补充说:

    android:elevation="5dp"
    

    直接到工具栏对我来说很好。

    【讨论】:

      猜你喜欢
      • 2020-05-04
      • 2018-01-03
      • 2019-08-15
      • 2016-04-30
      • 2017-12-23
      • 2014-12-21
      • 2023-03-27
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多