Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

material design - android:elevation only have shadow effects on the bottom side, how to make the shadow effects show on top side?

I use a framelayout at the bottom of an activity, in order to show the shadow effects on the fragment, I add android:elevation. But the shadow effects only appear in the bottom side not on top side, any one could give me some advice?

<FrameLayout
    android:id="@+id/bottom_container"
    android:background="#00737f"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom"
    android:elevation="4dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a trick that can be used to display a shadow above a View.

Basically we have to use two nested Layouts, where the outer Layout casts the shadow with an elevation and the inner layout sets the background. Then by setting a padding to the outer Layout, we can shift the inner Layout down, without moving the shadow, thus more of the shadow becomes visible:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:elevation="4dp"
    android:outlineProvider="bounds"
    android:paddingTop="2dp"
    android:layout_marginTop="-2dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00737f">

        <!-- content -->

    </FrameLayout>

</FrameLayout>

An important thing here is the attribute outlineProvider, that is required to make the outer Layout cast a shadow even without having a background set.

Further we specify a negative margin to compensate for the offset created by the padding. Depending on the use-case we can omit that.

But attention: If we shift the View too much, some rendering artifacts become visible:

android elevation shadow offset examples

Source of this example on Github


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...