我们如何为Android中的View设置阴影效果?

时间:2018-04-12 08:19:38

标签: android android-layout textview imageview android-view

在我的Android应用程序中,我有一个视图,它可能是ImageViewTextView如何在Android中为View设置阴影效果?我使用以下代码设置了阴影,但它不起作用:

view.setElevation(130)

请帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

您可以创建自己的“影子视图”

<View
android:id="@+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="bottom"
android:background="@drawable/shadow_gradient" />

并添加阴影drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="@android:color/transparent"
        android:startColor="#8f000000" />
</shape>

高度取决于你,试验并看看最适合你的是什么,以及可绘制的角度和起始颜色。你只需要将这个视图布局与你想拥有阴影的任何视图布局对齐,你就是好的。