首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android 9上没有工具栏提升阴影

在Android 9上没有工具栏提升阴影
EN

Stack Overflow用户
提问于 2019-05-07 13:28:13
回答 2查看 1K关注 0票数 1

我正在努力使工具栏阴影出现在安卓28+上。我尝试了多种解决方案,但都没有效果。以下样式适用于28岁以下的所有设备:

toolbar.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<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)

代码语言:javascript
复制
<style name="Widget.App.Toolbar" parent="Widget.App.Toolbar.V1">
    <item name="android:elevation">4dp</item>
</style>

我还尝试以编程方式设置高程,使用ViewCompat.setElevation,但没有成功。

有谁知道怎么让它工作吗?

UPD:我知道您可以创建自定义阴影绘图,但是如果可能的话,我想避免使用这个解决方案。只是不明白为什么这样的基本功能在布局中需要额外的可绘制和视图。另外,我想知道为什么它能在以前的Android版本上工作。没有找到任何证据证明这种行为已经改变。

EN

回答 2

Stack Overflow用户

发布于 2019-05-07 13:42:24

最后,我为工具栏设置了自己的下拉阴影,认为它可能对任何寻找它的人都有帮助:

代码语言:javascript
复制
<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/工具栏_下拉阴影:

代码语言:javascript
复制
<?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>

@颜色/颜色茜素

代码语言:javascript
复制
<color name="color_alizarin">#e74c3c</color>

票数 0
EN

Stack Overflow用户

发布于 2019-05-07 13:54:28

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

代码语言:javascript
复制
android:elevation="5dp"

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56023753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档