Toolbar not showing elevation in Android 9, API 28

2019-08-19 12:46发布

问题:

In my app, the Toolbar is not showing shadow at its bottom, my target API is from Android 6 M to Android 9 P, I am testing my app on device having Android 9 Pie.

There is a same question here but no answer.

toolbar.xml

<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp">
</androidx.appcompat.widget.Toolbar>

activity_main.xml

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <include layout="@layout/toolbar"/>
        <FrameLayout
            android:id="@+id/fragment_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <include layout="@layout/navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

回答1:

You can implement like this -

<android.support.design.widget.AppBarLayout
           android:id="@+id/appBarLayout"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           app:elevation="0dp">

     <androidx.appcompat.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:elevation="4dp">
 </androidx.appcompat.widget.Toolbar>