android:elevation仅在底部具有阴影效果,如何使阴影效果在顶部显示? [英] android:elevation only have shadow effects on the bottom side, how to make the shadow effects show on top side?

查看:3499
本文介绍了android:elevation仅在底部具有阴影效果,如何使阴影效果在顶部显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动底部使用框架布局,为了显示片段上的阴影效果,我添加了android:elevation.但是阴影效果只会出现在底部,而不会出现在顶部,有人可以给我一些建议吗?

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"/>

推荐答案

有一个技巧可以用来在视图上方显示阴影.

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

基本上,我们必须使用两个嵌套的布局,其中外部布局使用elevation投射阴影,内部布局设置background. 然后通过将padding设置到外部Layout,我们可以向下移动内部Layout,而无需移动阴影,因此更多的阴影变得可见:

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>

这里重要的是属性outlineProvider,即使没有背景设置,该属性也可以使外部Layout投射阴影.

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.

此外,我们指定一个负数margin来补偿填充产生的偏移量.根据使用情况,我们可以忽略它.

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:

在Github上此示例的来源

这篇关于android:elevation仅在底部具有阴影效果,如何使阴影效果在顶部显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆