简体   繁体   English

Android 海拔属性不起作用

[英]Android elevation property not working

Since upgrading to SDK 21 I've been trying to use the elevation property but it never seems to work.自从升级到 SDK 21 以来,我一直在尝试使用高程属性,但它似乎从未奏效。

I want to elevate a frame above others so I'm setting the following我想将一个框架提升到其他框架之上,所以我正在设置以下内容

android:elevation="4dp"

but there's no sign of shadow.但没有阴影的迹象。 I've tried buttons and framelayouts but not got any elevation我试过按钮和框架布局,但没有任何提升

So here's the full tag所以这是完整的标签

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/panel_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:elevation="4dp"
    >

Am I missing something else I need to add我是否缺少其他需要添加的东西

确保包含 FrameLayout 的容器的背景不透明。

According to this Z = Elevation + TranslationZ根据这个Z = Elevation + TranslationZ

so try this:所以试试这个:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:id="@+id/panel_card"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#ffffff"
  android:elevation="4dp"
  android:translationZ="4dp">

You need to add a background to it something like:您需要为其添加背景,例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/panel_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@drawable/dialog_background">

Instead of using white color.而不是使用白色。 Otherwise the shadows won't work.否则阴影将不起作用。 Also, is it not necessary to use android:translationZ attribute since that just adds to the elevation, but is not required if you don't want animation.此外,是否没有必要使用 android:translationZ 属性,因为它只会增加高程,但如果您不想要动画则不需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM