Android的"海拔"不显示阴影 [英] Android "elevation" not showing a shadow
问题描述
我有一个ListView,并与每个列表项我想让它显示在它下面一层阴影。我使用的是Android棒棒堂的新提升功能设置上,我希望蒙上了阴影查看一个Z,和我已经与动作条(技术上是一个工具栏的棒棒糖)有效地这样做。我使用棒棒糖的提升,但由于某些原因,它没有显示列表项下的阴影。下面是每个列表项的布局是如何设置的:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:方向=垂直
风格=@风格/块
机器人:重力=中心
机器人:layout_gravity =中心
机器人:背景=@色/浅灰色
>
< RelativeLayout的
机器人:layout_width =300dp
机器人:layout_height =300dp
机器人:layout_marginLeft =40dp
机器人:layout_marginRight =40dp
机器人:layout_marginTop =20dp
机器人:layout_marginBottom =20dp
机器人:海拔=30dp
>
< ImageView的
机器人:ID =@ + ID / documentImageView
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:scaleType =centerCrop/>
<的LinearLayout
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/ alphared
机器人:layout_alignParentBottom =真正的>
< appuccino.simplyscan.Extra.CustomTextView
机器人:ID =@ + ID / documentName
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@色/白
应用程序:字体=轻
机器人:以下属性来=16DP
机器人:paddingTop =8DP
机器人:paddingBottom会=4DP
机器人:单线=真
机器人:文本=新建文档
机器人:TEXTSIZE =27sp/>
< appuccino.simplyscan.Extra.CustomTextView
机器人:ID =@ + ID / documentPageCount
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@色/白
应用程序:字体=斜体
机器人:以下属性来=16DP
机器人:layout_marginBottom =16DP
机器人:文本=1页
机器人:TEXTSIZE =20SP/>
< / LinearLayout中>
< / RelativeLayout的>
< / RelativeLayout的>
不过,在这里是如何显示列表项,无阴影:
我也曾尝试以下无济于事:
- 将抬高到的ImageView和TextViews父布局,而不是自己。
- 应用背景的ImageView的。
- 在海拔的地方使用TranslationZ。
我一直在玩弄阴影棒棒糖了一下,这是我发现:
- 看来,一个父
的ViewGroup
的界限截止它的孩子由于某些原因的影子;和 - 设置为
Android的阴影:海拔
是截止由查看
的界限,没有界限,通过扩展保证金; - 在以正确的方式来获得一个子视图来显示阴影设置填充父,并设置
机器人:clipToPadding =假
在该一方李>
下面是我给你的建议基于我所知道的:
- 设置您的顶级
RelativeLayout的
有填充等于你想显示你的影子在相对布局已经设置页边距; - 设置
安卓clipToPadding =假
同一RelativeLayout的
; - 从
RelativeLayout的
还具有高度集中删除保证金; - 您可能还需要设置在需要抬高子布局的一个不透明的背景颜色。
在一天结束的时候,您的顶级相对布局应该是这样的:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
机器人:方向=垂直
风格=@风格/块
机器人:重力=中心
机器人:layout_gravity =中心
机器人:背景=@色/浅灰色
机器人:以下属性来=40dp
机器人:paddingRight40dp
机器人:paddingTop =20dp
机器人:paddingBottom会=20dp
机器人:clipToPadding =假
>
室内相对布局应该是这样的:
< RelativeLayout的
机器人:layout_width =300dp
机器人:layout_height =300dp
机器人:背景=[一些非透明色]
机器人:海拔=30dp
>
I have a ListView, and with each list item I want it to show a shadow beneath it. I am using Android Lollipop's new elevation feature to set a Z on the View that I want to cast a shadow, and am already doing this effectively with the ActionBar (technically a Toolbar in Lollipop). I am using Lollipop's elevation, but for some reason it isn't showing a shadow under the list items. Here is how each list item's layout is set up:
<?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>
However, here is how it shows the list item, without a shadow:
I have also tried the following to no avail:
- Set the elevation to the ImageView and TextViews themselves instead of the parent layout.
- Applied a background to the ImageView.
- Used TranslationZ in place of Elevation.
I've been playing around with shadows on Lollipop for a bit and this is what I've found:
- It appears that a parent
ViewGroup
's bounds cutoff the shadow of its children for some reason; and - shadows set with
android:elevation
are cutoff by theView
's bounds, not the bounds extended through the margin; - the right way to get a child view to show shadow is to set padding on the parent and set
android:clipToPadding="false"
on that parent.
Here's my suggestion to you based on what I know:
- Set your top-level
RelativeLayout
to have padding equal to the margins you've set on the relative layout that you want to show shadow; - set
android:clipToPadding="false"
on the sameRelativeLayout
; - Remove the margin from the
RelativeLayout
that also has elevation set; - [EDIT] you may also need to set a non-transparent background color on the child layout that needs elevation.
At the end of the day, your top-level relative layout should look like this:
<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"
android:paddingLeft="40dp"
android:paddingRight"40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"
>
The interior relative layout should look like this:
<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="[some non-transparent color]"
android:elevation="30dp"
>
这篇关于Android的&QUOT;海拔&QUOT;不显示阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!