高度引起的阴影在圆形背景中显示,但在矩形背景中不显示。

3
我正在尝试使用视图的高度属性来产生阴影。当背景是圆形或圆角矩形时,它可以正常工作。然而,如果我使用颜色或矩形可绘制对象作为背景,则阴影不会显示。
简单地将圆角矩形的半径更改为零会导致阴影消失。我已经尝试了类似问题的解决方案,例如添加填充和边距。我已经添加了两者:
android:clipChildren="false"
android:clipToPadding="false"

我尝试将内容传递给父视图,但那也没有解决问题。

我正在使用最简单的形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorAccent" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@color/colorAccent" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorAccent" />
</shape>

这是我测试时使用的非常简单的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/rectangle"
android:elevation="40dp"/>
</LinearLayout>

我知道在Android中实现阴影还有其他方法,比如使用9-patch图像,但是使用elevation是一种更简单的方式,我希望只使用这种方式来实现它。
这些是仅更改背景可绘制内容的预览。

https://i.imgur.com/CtVhI8m.png https://i.imgur.com/T2Ozcz7.png

这是我要达到的最终结果:

https://i.imgur.com/irIMNAF.png

但我主要想弄清楚为什么阴影不会显示出颜色或矩形可绘制对象作为背景。
2个回答

0
在我的情况下,将我的形状用 layer-list 包装起来有所帮助,就像这样:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/avatar_ripple">
<item>
    <shape android:shape="oval">
        <solid android:color="@color/black_light" />
        <stroke
            android:width="@dimen/stroke_large"
            android:color="@color/gray" />
    </shape>
</item>

然后,您可以将elevation应用于具有可绘制背景的视图,但是存在一个错误,即在启动应用程序之前您将看不到阴影。

特别感谢此答案:https://dev59.com/a1oU5IYBdhLWcg3w15ay#60424218


0

当我使用纯色背景(例如android:background="#fff")或没有圆角的矩形形状并运行应用程序时,我会看到一个阴影。

然而,我必须实际上运行应用程序。只是查看Android Studio布局预览不会显示阴影,除非我使用带有圆角的背景可绘制对象。因此,我怀疑您只是在查看布局预览的错误(或功能?)。


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