我有一个ListView,对于每个列表项,我希望它在它下面显示一个阴影。我正在使用Android棒棒糖的新提升功能来设置一个Z视图上,我想投射一个阴影,并且已经有效地与动作栏(技术上的棒棒糖工具栏)。我使用的是棒棒糖的抬高,但出于某种原因,它在列表项下没有显示阴影。下面是每个列表项的布局设置:

<?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>

然而,下面是它如何显示列表项,没有阴影:

我也尝试过以下方法,但都无济于事:

将提升设置为ImageView和TextViews本身,而不是父布局。 应用了ImageView的背景。 使用TranslationZ代替Elevation。


当前回答

添加背景色对我有帮助。

<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"

    />

其他回答

给布局一些背景色对我有用 如:

    android:background="@color/catskill_white"
    android:layout_height="?attr/actionBarSize"
    android:elevation="@dimen/dimen_5dp"

我有一些运气设置clipChildren="false"在父布局。

对我来说,设置立面、背景和边缘非常有效。

android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="@drawable/card_view" //any drawable or color
android:elevation="4dp"

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

似乎父ViewGroup的边界由于某种原因切断了子ViewGroup的阴影;而且 使用android设置的阴影:elevation被视图的边界截断,而不是通过边缘延伸的边界; 让子视图显示阴影的正确方法是在父视图上设置padding,并在父视图上设置android:clipToPadding="false"。

根据我所知道的,以下是我对你的建议:

设置你的顶级RelativeLayout,让padding等于你在相对布局上设置的边缘,你想要显示阴影; 在相同的RelativeLayout上设置android:clipToPadding="false"; 从同样设置了仰角的RelativeLayout中移除边缘; [编辑]你可能还需要在需要提升的子布局上设置一个非透明的背景色。

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

<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"
    >

我花了一些时间来处理它,最终意识到当背景是黑暗的时候,阴影是看不见的