我有一个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。


当前回答

如果你的视野没有背景,这条线会对你有帮助

android:outlineProvider="bounds"

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

其他回答

在我的案例中,字体是问题所在。

1)没有fontFamily我需要设置android:background的一些值。

<TextView
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:background="@android:color/white"
    android:elevation="3dp"
    android:gravity="center"
    android:text="@string/fr_etalons_your_tickets"
    android:textAllCaps="true"
    android:textColor="@color/blue_4" />

2)与fontFamily我必须添加android:outlineProvider="bounds"设置:

<TextView
    android:fontFamily="@font/gilroy_bold"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:background="@android:color/white"
    android:elevation="3dp"
    android:gravity="center"
    android:outlineProvider="bounds"
    android:text="@string/fr_etalons_your_tickets"
    android:textAllCaps="true"
    android:textColor="@color/blue_4" />

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

似乎父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"
    >

添加背景色对我有帮助。

<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:outlineProvider="bounds"

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

也检查你是否没有改变视图上的alpha。我正在调查问题,当我改变alpha在一些观点,与海拔。 当alpha被改变并变回1f时,它们只是松散的高程。