【问题标题】:Elevation property not working android海拔属性不起作用android
【发布时间】:2015-01-11 11:02:15
【问题描述】:

您好,我正在开发 android 应用程序,我正在尝试实现新的材料设计功能。我试图应用海拔属性,但它不适合我。我尝试了以下方式。

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

【问题讨论】:

标签: android material-design android-elevation


【解决方案1】:

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

【讨论】:

  • @luiiO 感谢您的快速重播。我在 android 版本 5.0 的模拟器上尝试了上面的代码,但仍然无法正常工作。
  • 对不起,我的错误,它应该适用于 ImageButton,而不是 ImageView(在运行 5.0 的 Nexus 7 上尝试过)。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2019-01-03
    • 2018-03-05
    • 2014-12-25
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多