安卓XML:阴影被截断

25

我有一个相对布局,里面有一个带边距的浮动操作按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activityMargin"
            android:orientation="vertical"
            android:clipToPadding="false">


<android.support.design.widget.FloatingActionButton
    android:id="@+id/id_FABSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    app:srcCompat="@drawable/ic_save_white"/>

</RelativeLayout>

如图所示,浮动操作按钮的投影被裁剪了。为什么会发生这种情况,应该如何修复?

Bottom and right side of shadow cut off

2个回答

45

在你的相对布局标签中,使用padding而不是margin,并添加属性 android:clipToPadding="false" 以避免阴影被切割。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/activityMargin"
        android:clipToPadding="false">

1
我尝试过了,可惜它不起作用。我会编辑我的帖子。 - Vancore
2
哦,因为是 layout_margin!我已经更新了我的回答。 - Lewis McGeary

5

问题是阴影被视图或视图组的边界所遮挡。为了解决这个问题,您需要使用:

android:clipChildren

定义一个子元素是否被限制在其边界内绘制。
android:clipToPadding

定义ViewGroup是否剪裁其子View,并调整(但不剪裁)任何EdgeEffect到其padding大小,如果padding不为零。

问题是,如果您想要渲染阴影,则必须在xml中为许多视图设置此选项。我在themes.xml级别上解决了这个问题。在我的顶级主题中,我只需设置:

<item name="android:clipChildren">false</item>
<item name="android:clipToPadding">false</item>

然后,如果屏幕上有空间,阴影就会被渲染。我希望它不会破坏其他东西。

编辑:它会破坏一些视图。例如,相机预览将整个屏幕设置为黑色背景。对于可滚动的视图等,请小心使用。


1
这应该是正确的答案,clipToPadding并不总是足够的。clipChildren和clipToPadding两者都对我有用。 - Anurban
1
谢谢,但是要小心那些属性。它们可能会破坏事物的视觉效果。 - Michal Zhradnk Nono3551

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接