我正在尝试使用视图的高度属性来产生阴影。当背景是圆形或圆角矩形时,它可以正常工作。然而,如果我使用颜色或矩形可绘制对象作为背景,则阴影不会显示。
简单地将圆角矩形的半径更改为零会导致阴影消失。我已经尝试了类似问题的解决方案,例如添加填充和边距。我已经添加了两者:
我知道在Android中实现阴影还有其他方法,比如使用9-patch图像,但是使用elevation是一种更简单的方式,我希望只使用这种方式来实现它。
这些是仅更改背景可绘制内容的预览。
简单地将圆角矩形的半径更改为零会导致阴影消失。我已经尝试了类似问题的解决方案,例如添加填充和边距。我已经添加了两者:
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
这是我要达到的最终结果:
但我主要想弄清楚为什么阴影不会显示出颜色或矩形可绘制对象作为背景。