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

查看:105
本文介绍了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.然后通过为外层 Layout 设置 padding ,我们可以将内层 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天全站免登陆