Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elevation on AppBarLayout doesn't work

When I try to set a specific value to elevation for AppBarLayout, the shadow disappears completely.

 <android.support.design.widget.AppBarLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:elevation="4dp">

     <!-- Toolbar -->
     <android.support.v7.widget.Toolbar...

     <!-- Other Layouts -->

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

Is this a bug or the expected behaviour?

I'm using the version 26.0.0 of the design library.

like image 946
marc97 Avatar asked Aug 16 '17 00:08

marc97


2 Answers

Setting Property Animation

Creating an animation with 1ms of execution time:/animator/appbar_elevation.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <objectAnimator
        android:duration="1"
        android:propertyName="elevation"
        android:valueTo="2dp"
        android:valueType="floatType" />
</item>
</selector>

Setting it to AppBarLayout

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"    
    android:stateListAnimator="@animator/appbar_elevation">
</android.support.design.widget.AppBarLayout>

And it can use in java code.

appBarLayout.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.animator.appbar_elevation));
like image 62
KeLiuyue Avatar answered Oct 07 '22 02:10

KeLiuyue


Work for me:

    ViewCompat.setElevation(appBarLayout, getResources().getDimension(R.dimen.toolbar_elevation));
like image 42
UgAr0FF Avatar answered Oct 07 '22 00:10

UgAr0FF