使用28 API的设备上未显示工具栏阴影

时间:2019-07-18 08:27:57

标签: android android-toolbar shadow

我有以下问题:我需要为Toolbar做阴影。我读到可以使用以下xml属性来做到这一点:app:elevation="20dp"。我设置一个很大的值只是为了看看是否存在阴影。但是如您所见,没有阴影:

enter image description here

所以,我不明白这是怎么回事,因为我的设备具有API 28(> 21,不支持此属性。

这是我的布局代码:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.login.LoginActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:elevation="20dp">

        <TextView
            android:id="@+id/tv_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="#FFFFFF"
            android:textSize="24sp" />

    </androidx.appcompat.widget.Toolbar>

</androidx.constraintlayout.widget.ConstraintLayout>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用:

android:elevation="20dp"

还是在工具栏上添加黑色或灰色的背景色,看是否可行?