Search code examples
androidandroid-fragmentsandroid-5.0-lollipop

android:Elevation not respected across FragmentTransactions


So I have a layout which looks similar to the following:

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:elevation="4dp"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"/>

         <FrameLayout
             android:id="@+id/content"
             android:layout_below="@+id/toolbar"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>

    </RelativeLayout>

This forms my base layout file for my activity and then I swap out the FrameLayout for various fragments. One such fragment has a layout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:elevation="4dp"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/profileViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/sliding_tabs"
        android:background="@android:color/white"/>

    <FloatingActionButton
        android:id="@+id/floatingButton"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"/>

</RelativeLayout>

I would expect that as I have set the elevation of both the Toolbar and the SlidingTabLayout to be 4dp, that the toolbar would not cast a shadow on the tabs, however it does:

Screenshot

Ideally I wouldn't want to be setting the elevation of the Toolbar in code - does anyone know if there is a way to prevent the Toolbar casting a shadow? I would have assumed that as both views are at 4dp elevation, even though they aren't in the same layout, no shadow would be cast.

Thanks for the help.


Solution

  • Shadows respect the elevation of siblings, but not cousins. The action bar and the SlidingTabLayout are not siblings, so the action bar casts a shadow on the SlidingTabLayout.

    You'll need to customize the action bar to remove the elevation if you don't want the shadow.