Android Lollipop 上的海拔不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26572048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:52:37  来源:igfitidea点击:

Elevation on Android Lollipop not working

androidandroid-5.0-lollipop

提问by Moritz

I am trying to make use of the elevation property in the latest Android Lollipop preview release. I set the targetSdk to 21 and the theme to Material. Next i added a background shape to a TextView and set the elevation to 8dp but the TextView is not showing any signs of a shadow. That is on a Nexus7 running the Lollipop preview. Is there anything else i have to consider?

我正在尝试在最新的 Android Lollipop 预览版中使用高程属性。我将 targetSdk 设置为 21,将主题设置为 Material。接下来,我向 TextView 添加了背景形状并将高程设置为 8dp,但 TextView 没有显示任何阴影迹象。那是在运行 Lollipop 预览的 Nexus7 上。还有什么我需要考虑的吗?

Here is the layout:

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/rect"
      android:text="hallo world"
      android:padding="8dp"
      android:elevation="8dp" />

</LinearLayout>

This is the background drawable:

这是可绘制的背景:

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

Here is the TextView:

这是文本视图:

Android elevation

安卓海拔

回答by rlay3

For some reason if you set a solid color with a transparency, the elevation shadow does not show up.

出于某种原因,如果您设置具有透明度的纯色,则不会显示高程阴影。

In your example, I changed #7d0073ff to #0073ff and I got a shadow.

在您的示例中,我将 #7d0073ff 更改为 #0073ff 并且我得到了一个阴影。

This is probably a bug, as in their documentationit gives a similar example using a translucent background color.

这可能是一个错误,因为在他们的文档中它给出了一个使用半透明背景颜色的类似示例。

回答by Ram Patra

After going through the docs again, I finally found the solution.

再次浏览文档后,我终于找到了解决方案。

Just add card_view:cardUseCompatPadding="true"to your CardViewand shadows will appear on Lollipop devices.

只需添加card_view:cardUseCompatPadding="true"到您的CardView阴影就会出现在 Lollipop 设备上。

What happens is, the content area in a CardViewtake different sizes on pre-lollipop and lollipop devices. So in lollipop devices the shadow is actually covered by the card so its not visible. By adding this attribute the content area remains the same across all devices and the shadow becomes visible.

发生的情况是,CardView在预棒棒糖和棒棒糖设备上,内容区域的大小不同。所以在棒棒糖设备中,阴影实际上被卡片覆盖,所以它不可见。通过添加此属性,所有设备的内容区域都保持不变,并且阴影变得可见。

My xml code is like :

我的 xml 代码是这样的:

<android.support.v7.widget.CardView
    android:id="@+id/media_card_view"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardElevation="2sp"
    card_view:cardUseCompatPadding="true"
    >
...
</android.support.v7.widget.CardView>

回答by Informatheus

ADDING android:elevation shadow to an ImageView:

将 android:elevation 阴影添加到 ImageView:

android:clipToPadding="false"
+
android:outlineProvider="bounds"
+
android:elevation="2dp"

回答by vcapra1

I was also having this problem, and as it turns out, you need to turn hardware acceleration on in the android manifest

我也遇到了这个问题,事实证明,您需要在 android manifest 中打开硬件加速

<application
  ...
  android:hardwareAccelerated="true">

回答by nada

be aware, if you have the following line in the manifest then shadows wont show:

请注意,如果清单中有以下行,则阴影不会显示:

android:hardwareAccelerated="false"

机器人:硬件加速=“假”

回答by lustig

TL;DR

TL; 博士

Check your card: (or whatever word you use) namespace declaration and make sure it matches this: xmlns:card="http://schemas.android.com/apk/res-auto"

检查您的卡片:(或您使用的任何单词)命名空间声明并确保它与此匹配:xmlns:card="http://schemas.android.com/apk/res-auto"

I know there are a few answers here already, but I wanted to add mine as it wasn't included in these current suggestions. In order to get the shadows working on both KitKat and Marshmallow, (only emulators I tried, I'm sure it works in between) I added the following xml attributes to my card:

我知道这里已经有一些答案,但我想添加我的答案,因为它没有包含在这些当前的建议中。为了让阴影在 KitKat 和 Marshmallow 上都能工作(我只试过模拟器,我确定它可以在两者之间工作)我在我的卡片中添加了以下 xml 属性:

card:cardElevation="25dp"
card:cardUseCompatPadding="true"

After banging my head against my desk for why that wasn't working, trying to set the background color of the card to something completely opaque, enabling hardware acceleration in the manifest, and even praying, I checked the namespace declarations in my file. To my horror, I saw that the card xml namespace had been assigned to the following:

在我的头撞在我的桌子上为什么这不起作用之后,尝试将卡的背景颜色设置为完全不透明的东西,在清单中启用硬件加速,甚至祈祷,我检查了我的文件中的命名空间声明。令我震惊的是,我看到 card xml 命名空间已分配给以下内容:

xmlns:card="http://schemas.android.com/tools"

After fixing that namespace declaration, I ran my dummy app again and let out a sigh of relief as shadows were finally being shown as expected.

修复了命名空间声明后,我再次运行了我的虚拟应用程序,当阴影终于按预期显示时,我松了一口气。

Here is proof in case you're a doubter like me. And please Google, Android, whoever: Make shadows great again. They shouldn't be this difficult.

如果你像我一样怀疑,这里有证据。请谷歌、安卓系统,无论是谁:让阴影再次变得伟大。他们不应该这么难。

enter image description here

在此处输入图片说明

Here is the entire file that created the layout pictured below:

这是创建下图所示布局的整个文件:

<?xml version="1.0" encoding="utf-8"?>

<!--MAKE SURE YOU HAVE THE RIGHT XML NAMESPACE ASSIGNED-->
<!--TO WHATEVER WORD YOU PUT IN FRONT OF THE CARD-->
<!--XML ATTRIBUTES. IN THIS CASE, MINE IS card-->
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#607D8B"
    android:fitsSystemWindows="true"
    tools:context="com.mlustig.playground.MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        app:layout_aspectRatio="66%"
        app:layout_heightPercent="75%"
        card:cardElevation="25dp"
        card:cardUseCompatPadding="true" />
</android.support.percent.PercentRelativeLayout>

Yes, I know. It's annoying that you can't simply copy and paste this layout and run it because it has PercentRelativeLayout in it, but I left it there on purpose. You should definitely checkit out. Super powerful, very useful. Nice nice nice. Hope this helped.

是的我知道。令人讨厌的是,您不能简单地复制和粘贴此布局并运行它,因为其中包含 PercentRelativeLayout,但我故意将其留在那里。你一定要检查。超级强大,非常好用。不错不错不错。希望这有帮助。

回答by Samuel Peter

As noted earlier, this is an open bugin Android : if the background drawable uses a solid color with transparency, then the shadow won't be shown.

如前所述,这是Android 中的一个开放错误:如果可绘制背景使用具有透明度的纯色,则不会显示阴影。

To work around the issue, display the background in its own separate view and set the alpha on that view. Wrap the background view and the TextViewin a RelativeLayoutto place the background directly under the TextViewand use android:layout_alignLeft, android:layout_alignBottometc to make it the same size. They need to be at the same elevation, and the background must appear before the TextViewin the xml so it is drawn under it.

要解决此问题,请在单独的视图中显示背景并在该视图上设置 alpha。包裹背景视图和TextView一个RelativeLayout在后台直接放在下TextView和使用android:layout_alignLeftandroid:layout_alignBottom等等,使其大小相同。它们需要处于相同的高度,并且背景必须出现在TextViewxml之前,以便在其下方绘制。

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:clipToPadding="false"
    android:background="#ffffff">

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text_view"
        android:layout_alignRight="@id/text_view"
        android:layout_alignBottom="@id/text_view"
        android:layout_alignTop="@id/text_view"
        android:background="@drawable/rect"
        android:alpha="0.5"
        android:elevation="8dp"/>


    <TextView
        android:id="@id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hallo world"
        android:textColor="#ffffff"
        android:padding="8dp"
        android:elevation="8dp" />

</RelativeLayout>

The drawable is the same as yours but without transparency:

drawable 与您的相同,但没有透明度:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#0073ff" />
    <corners android:radius="16dp" />
</shape>

Result:

结果:

Result

结果

A few notable points:

几个值得注意的点:

  • You must ensure that the enclosing RelativeLayoutis large enough to display the shadow. If you just set its dimensions to wrap_contentwithout padding, the shadow will be clipped to the layout's bounds. As explained in this question, you can use padding and set android:cipToPadding="false"to make it large enough for the shadow. You might not need as much padding in this case, I haven't experimented with that.
  • In this case we need to use a separate view for the background because if we had set the alphadirectly on the TextViewthen the text would have been affected as well. Depending on your use case you might not need a separate view and enclosing layout and could just just have one view, with alphaset on the view and no transparency in the drawable.
  • 您必须确保封闭RelativeLayout区域足够大以显示阴影。如果您只是将其尺寸设置为wrap_content无填充,则阴影将被裁剪到布局的边界。正如在这个问题中所解释的,您可以使用填充并设置android:cipToPadding="false"使其足够大以容纳阴影。在这种情况下,您可能不需要那么多的填充,我还没有尝试过。
  • 在这种情况下,我们需要为背景使用单独的视图,因为如果我们alpha直接在 上设置,TextView那么文本也会受到影响。根据您的用例,您可能不需要单独的视图和封闭布局,并且可以只有一个视图,alpha在视图上设置并且在可绘制对象中没有透明度。

回答by imran.razak

Try using : app:cardElevation="4dp"

尝试使用: app:cardElevation="4dp"

回答by Sam Chen

Try adding margin to the CardView.

尝试向CardView.