高程不适用于ImageView

时间:2015-06-04 09:57:42

标签: android imageview android-elevation

ImageView的提升无法正常工作。我在XML中声明了ImageView:

<ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:elevation="10dp"
        android:src="@drawable/youtube" />

我还应该做些什么来提升ImageView的正常工作?

8 个答案:

答案 0 :(得分:25)

高程阴影源自View的背景可绘制。如果您的ImageView没有背景,您将看不到阴影。

如果您想要更改该行为,则需要构建自己的ViewOutlineProvider并调用View.setOutlineProvider()进行设置(这并非易事)。

答案 1 :(得分:9)

适用于所有最新版本

将CardView用作父版面。在ImageView中使用android:background="@drawable/your_img"代替android:src="@drawable/your_img"

<android.support.v7.widget.CardView
    android:layout_gravity="center_horizontal"        
    app:cardElevation="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView       
        android:id="@+id/image"
        android:background="@drawable/your_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null" />

</android.support.v7.widget.CardView>

答案 2 :(得分:2)

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

 <ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:elevation="10dp"
    android:background="@drawable/youtube" />

答案 3 :(得分:2)

如果您使用的是CardView,请确保将CardView背景保持透明。

<android.support.v7.widget.CardView
                android:layout_gravity="center_horizontal"
                app:cardElevation="@dimen/cardview_default_elevation"
                app:cardBackgroundColor="@android:color/transparent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
<ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:src="@drawable/youtube" />

</android.support.v7.widget.CardView>

答案 4 :(得分:2)

为了使用带有高程属性的SHADOW显示圆形图像,我创建了一个圆形可绘制的WITHOUT SHADOW,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <solid android:color="@android:color/white" />
        </shape>
    </item>

</layer-list>

在ImageView中将此drawable用作背景,如下所示:

    <ImageView
        android:id="@+id/my_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_shape"
        android:elevation="5dp"
        android:src="@drawable/center" />

这将显示带阴影的ImageView。

在您的脑海中可能出现的一个问题是为什么要使用xml drawable。正如其他开发人员对这个问题的各种答案所提到的,阴影是由背景和带边框的背景形成的,以便更加具体。 Xml drawable帮助我们创建一个有边框的背景,高程属性可以从中创建阴影。

答案 5 :(得分:0)

Lollipop上,它可以设置高程。对于pre-Lollipop,,您需要自己实现阴影。如果您检查了源代码,则Support-v4具有ViewCompat.setElevation(view, value)方法,但没有实现。 (至少在撰写本文时)。

答案 6 :(得分:0)

作为对BladeCoder's answer的补充:将android:outlineProvider="bounds"应用于ImageView将导致阴影显示。

答案 7 :(得分:0)

如果您的minSdk版本小于21,则将忽略海拔高度属性。要么对此进行更改,要么尝试使用此处介绍的方法https://stackoverflow.com/a/28980908/7184172

enter image description here