Elevation属性在安卓上无效。

4

你好,我正在开发一款Android应用程序,尝试实现新的Material Design功能。我试图应用高程属性,但它对我没有起作用。我是以下面的方式尝试的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.example.androidmaterialsamples.MainActivity" >
<ImageView 
    android:id="@+id/sampleImage"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_centerInParent="true"
    android:elevation="10dp"/>
 </RelativeLayout>

那张图片没有显示任何的阴影,我做错了什么吗?需要帮助,谢谢。

我也试过这种方法,但输出结果还是没有改变。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<solid android:color="#0073ff" />
<corners android:radius="16dp" />
</shape>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.example.androidmaterialsamples.MainActivity" >
<TextView 
    android:id="@+id/sampleImage"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="my name is nilesh"
    android:padding="10dp"
    android:textSize="20sp"
    android:layout_centerInParent="true"
    android:elevation="12dp"
    android:background="@drawable/background"/>
</RelativeLayout>

你更新了最新的API吗? - iffu
尝试给一些不透明的背景颜色或图像。 - Chk0nDanger
可能是Android AppCompat 21 Elevation的重复问题。 - Derk-Jan
2个回答

1

不要使用android:src,而是改用android:background来显示阴影。

<ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:background="@drawable/ic_launcher"/>

0
在Lollipop上,可以通过设置高度来实现阴影效果。但是在Lollipop之前的版本中,您需要自己实现阴影效果。 Support-v4库提供了ViewCompat.setElevation(view, value)方法,但是如果您查看源代码,会发现没有实现(至少在撰写本文时如此)。

@luriiO 谢谢您的快速回复。我在安卓5.0模拟器上尝试了上述代码,但仍然无法正常工作。 - nilkash
抱歉,我的错误,它应该适用于ImageButton而不是ImageView(在运行5.0的Nexus 7上尝试过)。 - IuriiO

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