Android“海拔”没有出现阴影

我有一个ListView,并与每个列表项我希望它显示一个阴影。 我正在使用Android Lollipop的新高程特性在View上设置一个Z来投射阴影,并且已经使用ActionBar(技术上来说是Lollipop中的一个工具栏)来有效地执行此操作。 我正在使用棒棒糖的海拔,但由于某种原因,它没有在列表项下显示阴影。 以下是每个列表项的布局设置:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" style="@style/block" android:gravity="center" android:layout_gravity="center" android:background="@color/lightgray" > <RelativeLayout android:layout_width="300dp" android:layout_height="300dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:elevation="30dp" > <ImageView android:id="@+id/documentImageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/alphared" android:layout_alignParentBottom="true" > <appuccino.simplyscan.Extra.CustomTextView android:id="@+id/documentName" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" app:typeface="light" android:paddingLeft="16dp" android:paddingTop="8dp" android:paddingBottom="4dp" android:singleLine="true" android:text="New Document" android:textSize="27sp"/> <appuccino.simplyscan.Extra.CustomTextView android:id="@+id/documentPageCount" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/white" app:typeface="italic" android:paddingLeft="16dp" android:layout_marginBottom="16dp" android:text="1 page" android:textSize="20sp"/> </LinearLayout> </RelativeLayout> </RelativeLayout> 

但是,下面是它如何显示列表项目,没有阴影: 在这里输入图像描述

我也试过以下无效:

  1. 将高程设置为ImageView和TextView而不是父级布局。
  2. 将背景应用于ImageView。
  3. 使用TranslationZ代替Elevation。

我一直在玩棒棒糖上的阴影,这是我发现的:

  1. 由于某种原因, ViewGroup的一个界限看起来ViewGroup截断了子女的影子; 和
  2. android:elevation设置的阴影被View的边界截断,而不是通过边界延伸的边界;
  3. 获取子视图以显示阴影的正确方法是在父级上设置填充,并在该父级上设置android:clipToPadding="false"

这是我的build议,基于我所知道的:

  1. 将顶层的RelativeLayout为使填充等于您在相对布局上设置的边距,以便显示阴影;
  2. 在同一个RelativeLayout上设置android:clipToPadding="false" ;
  3. 从具有高程设置的RelativeLayout中移除边距;
  4. [编辑]你可能还需要在需要提升的子布局上设置一个不透明的背景颜色。

在一天结束时,您的顶层相对布局应如下所示:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" style="@style/block" android:gravity="center" android:layout_gravity="center" android:background="@color/lightgray" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="20dp" android:paddingBottom="20dp" android:clipToPadding="false" > 

内部相对布局应该是这样的:

 <RelativeLayout android:layout_width="300dp" android:layout_height="300dp" android:background="[some non-transparent color]" android:elevation="30dp" > 

如果你有一个没有背景的视图,这行会帮助你

 android:outlineProvider="bounds" 

默认情况下,阴影是由视图的背景决定的,所以如果没有背景,也不会有阴影。

显然,你不能只是在一个视图上设置一个高程并显示它。 您还需要指定背景。

添加到我的LinearLayout的以下行最后显示了一个阴影:

 android:background="@android:color/white" android:elevation="10dp" 

另外一件你应该知道的事情,如果你在清单中有这条线,阴影就不会显示:

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

我尝试了所有build议的东西,但它只适用于我,当我删除线,原因我有线是因为我的应用程序与一些位图图像的作品,他们导致应用程序崩溃。

呃,花了一个小时试图弄清楚。 在我的情况下,我有一个背景设置,但它被设置为一种颜色。 这还不够,你需要把视图的背景设置为drawable。

例如:这不会有阴影:

 <LinearLayout android:layout_width="match_parent" android:layout_height="165dp" android:background="@color/ight_grey" android:elevation="20dp" android:orientation="vertical" /> 

但是这将会

 <LinearLayout android:layout_width="match_parent" android:layout_height="165dp" android:background="@drawable/selector_grey" android:elevation="20dp" android:orientation="vertical" /> 

编辑:也发现,如果你使用select器作为背景,如果你没有设置angular落,那么阴影不会出现在预览窗口,但它会出现在设备上

例如,这在预览中没有影子:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/white" /> <stroke android:width="1dp" android:color="@color/grey"/> </shape> </item> </selector> 

但是这样做:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:radius="1dp" /> <stroke android:width="1dp" android:color="@color/grey"/> </shape> </item> </selector> 

添加背景颜色帮助了我。

 <CalendarView android:layout_width="match_parent" android:id="@+id/calendarView" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_height="wrap_content" android:elevation="5dp" android:background="@color/windowBackground" /> 

如果要将高程添加到button元素,则需要添加以下行:

 android:stateListAnimator="@null" 

请参阅Android 5.0 android:提升视图,但不是button?

除了被接受的答案之外,还有一个原因是由于哪个海拔可能无法按预期工作。

如果手机上的Bluelightfilterfunction处于开启状态

当我的设备中的高程出现完全扭曲和无法忍受的时候,我正面临着这个问题。 但预览中的海拔很高。 closures手机上的Bluelightfilter可以解决问题。

所以即使设置后

 android:outlineProvider="bounds" 

海拔不能正常工作,那么你可以尝试修补手机的颜色设置。

如果你想有透明的背景和android:outlineProvider="bounds"不适合你,你可以创build自定义的ViewOutlineProvider:

 class TransparentOutlineProvider extends ViewOutlineProvider { @Override public void getOutline(View view, Outline outline) { ViewOutlineProvider.BACKGROUND.getOutline(view, outline); Drawable background = view.getBackground(); float outlineAlpha = background == null ? 0f : background.getAlpha()/255f; outline.setAlpha(outlineAlpha); } } 

并将此提供者设置为您的透明视图:

 transparentView.setOutlineProvider(new TransparentOutlineProvider()); 

来源: https : //code.google.com/p/android/issues/detail?id = 78248#c13

[编辑] Drawable.getAlpha()的文档说,它是具体的如何可绘制的威胁阿尔法。 它可能总是返回255.在这种情况下,您可以手动提供alpha:

 class TransparentOutlineProvider extends ViewOutlineProvider { private float mAlpha; public TransparentOutlineProvider(float alpha) { mAlpha = alpha; } @Override public void getOutline(View view, Outline outline) { ViewOutlineProvider.BACKGROUND.getOutline(view, outline); outline.setAlpha(mAlpha); } } 

如果您的视图背景是一个StateListDrawable与默认颜色#DDFF0000与alpha DDx0 / FFx0 == 0.86

 transparentView.setOutlineProvider(new TransparentOutlineProvider(0.86f)); 

有时像背景=“@颜色/ *”或背景=“#ff33 ”不工作,我用drawablereplacebackground_color,然后工作

我相信你的问题源于这样一个事实,即你已经将高程元素应用到填充其父项的相对布局。 它的父级将自己的绘图canvas中的所有子视图剪辑(并且是处理孩子阴影的视图)。 您可以通过将高程属性应用于视图xml(根标记)中最顶层的RelativeLayout来解决此问题。