Recently met a demand in the project, AppBarLayout no shadow when fully expanded, showing shadows when fully retracted, this function can be easily achieved by setting the StateListAnimator.
First, create appbar_elevation.xml file under res / animator directory, as follows:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <!--折叠状态下的阴影--> <item app:state_collapsed="true"> <objectAnimator android:propertyName="elevation" android:valueTo="5dp" android:valueType="floatType" /> </item> <!- Item<->expand the shadow state app:state_collapsed="false"> <objectAnimator android:propertyName="elevation" android:valueTo="0dp" android:valueType="floatType" /> </item> </selector>
Then set AppBarLayout in the layout file stateListAnimator property as follows:
<android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="256dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:background="@color/white" android:stateListAnimator="@animator/appbar_elevation" >