Android的"海拔"不显示阴影 [英] Android "elevation" not showing a shadow

查看:215
本文介绍了Android的"海拔"不显示阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,并与每个列表项我想让它显示在它下面一层阴影。我使用的是Android棒棒堂的新提升功能设置上,我希望蒙上了阴影查看一个Z,和我已经与动作条(技术上是一个工具栏的棒棒糖)有效地这样做。我使用棒棒糖的提升,但由于某些原因,它没有显示列表项下的阴影。下面是每个列表项的布局是如何设置的:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://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的>
 

不过,在这里是如何显示列表项,无阴影:

我也曾尝试以下无济于事:

  1. 将抬高到的ImageView和TextViews父布局,而不是自己。
  2. 应用背景的ImageView的。
  3. 在海拔的地方使用TranslationZ。
解决方案

我一直在玩弄阴影棒棒糖了一下,这是我发现:

  1. 看来,一个父的ViewGroup 的界限截止它的孩子由于某些原因的影子;和
  2. 设置为 Android的阴影:海拔是截止由查看的界限,没有界限,通过扩展保证金;
  3. 在以正确的方式来获得一个子视图来显示阴影设置填充父,并设置机器人:clipToPadding =假在该一方

下面是我给你的建议基于我所知道的:

  1. 设置您的顶级 RelativeLayout的有填充等于你想显示你的影子在相对布局已经设置页边距;
  2. 设置安卓clipToPadding =假同一 RelativeLayout的;
  3. RelativeLayout的还具有高度集中删除保证金;
  4. 您可能还需要设置在需要抬高子布局的一个不透明的背景颜色。

在一天结束的时候,您的顶级相对布局应该是这样的:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://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:

  1. Set the elevation to the ImageView and TextViews themselves instead of the parent layout.
  2. Applied a background to the ImageView.
  3. 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:

  1. It appears that a parent ViewGroup's bounds cutoff the shadow of its children for some reason; and
  2. shadows set with android:elevation are cutoff by the View's bounds, not the bounds extended through the margin;
  3. 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:

  1. 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;
  2. set android:clipToPadding="false" on the same RelativeLayout;
  3. Remove the margin from the RelativeLayout that also has elevation set;
  4. [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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆