android工具栏无法显示阴影

时间:2017-07-05 08:20:28

标签: android xml toolbar shadow

这是我的xml代码: ```

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="xxxxxxxxx">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppThemeMD.NoActionBar.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:theme="@style/AppThemeBase"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_vehicle_info"/>
</android.support.design.widget.CoordinatorLayout>

```

1.当我运行我的应用程序时。我看不到工具栏的阴影。 enter image description here

2.但是当我切换到另一个应用程序时,然后切换回我的应用程序。阴影出现了。 enter image description here enter image description here

无论如何要显示影子默认值?thx!

这是我的手机:华为6x,Android7.0

3 个答案:

答案 0 :(得分:2)

将此添加到AndroidManifest.xml

<application
  ...
  android:hardwareAccelerated="true">

并在toobar.xml中添加:

android:clipToPadding="false"

答案 1 :(得分:1)

把它放在你的代码中(onCreate())

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Call some material design APIs here
    toolbar.setElevation(3) 
} 

答案 2 :(得分:0)

您应该将android:elevation = "4dp"添加到工具栏中,以便将其提升为:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppThemeMD.NoActionBar.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:elevation = "4dp"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        app:theme="@style/AppThemeBase"/>

</android.support.design.widget.AppBarLayout>