如何在Android中设置视图的阴影效果?

如何在Android中设置视图的阴影效果?,android,android-layout,textview,imageview,android-view,Android,Android Layout,Textview,Imageview,Android View,在我的Android应用程序中,我有一个视图,它可能是ImageView或TextView如何在Android中设置视图的阴影效果?我已经使用以下代码设置了阴影,但它不起作用: view.setElevation(130) 请帮我解决这个问题您可以创建自己的“阴影视图” 并添加可绘制的阴影: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/

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

view.setElevation(130)

请帮我解决这个问题

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


并添加可绘制的阴影:

<?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>


高度取决于您,您可以尝试并查看最适合您的是可拉丝的角度和起始颜色。您只需将此视图布局与希望有阴影的任何视图布局对齐即可。谢谢,我需要通过编程添加阴影,因为视图正在通过。我需要为被触摸的人设置阴影view@Resmi默认情况下,只需将视图保持为隐藏状态
view.setVisibility(view.INVISIBLE)
,当检测到向下触摸时,将阴影的可见性设置为可见。打开键控开关返回不可见:)
<?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>