在 AppBarLayout 上投影

Drop shadow on AppBarLayout

我已经使用设计支持库的 AppBarLayout 在我的项目中显示 Toolbar 和 TabLayout。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:background="#FF0000">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:scrollbars="horizontal"
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />

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

此 AppBarLayout 在 Lollipop 中投射阴影,但在 pre-lollipop 设备中没有阴影。

如何在棒棒糖之前的设备中也显示阴影?

请注意,这仅适用于棒棒糖及更高版本。它在棒棒糖设备之前不起作用,因此您必须为阴影创建自定义 xml,如下所示。

<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 xmlns:android="http://schemas.android.com/apk/res/android"
        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>
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@android:color/transparent"
            android:fitsSystemWindows="true">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

            <application.project.freelincerapp.tabs.SlidingTabLayout
                android:id="@+id/slidingTabLayout"
                android:layout_width="match_parent"
                android:fitsSystemWindows="true"
                android:layout_height="wrap_content" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/appBarLayout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <View
            android:id="@+id/toolbar_shadow"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:alpha="0.90"
            android:layout_below="@id/appBarLayout"
            android:background="@drawable/custome_shadow"/>


    </RelativeLayout>

在drawable下创建一个xml文件并命名为custom_shadow.xml。现在复制并粘贴以下代码..

<gradient
    android:centerColor="@android:color/transparent"
    android:endColor="@android:color/transparent"
    android:startColor="@android:color/transparent" />

<corners android:radius="0dp" />

<FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/ab_shadow_height"
                android:background="@drawable/sh_bar" />
        </FrameLayout>

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