Android海拔-相同的值不同的阴影

The*_*dge 4 android shadow android-cardview android-elevation android-recyclerview

我在回收器视图中设置项目标高时遇到了一个奇怪的错误。我在适配器中有两种项目类型,第一种是一种具有复杂布局的仪表板,具有三个cardview。第二个是数据列表的经典项目,其中每个项目都以卡片视图作为根。两种布局中的每个卡片视图定义如下

第一个项目类型:

<LinearLayout
        android:id="@+id/root"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:cardCornerRadius="2dp"
            app:cardElevation="3dp"
            >

        ....

    </android.support.v7.widget.CardView>

    ....

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

第二个项目类型:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:cardCornerRadius="2dp"
        app:cardElevation="3dp"
        >

    ....

</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,两个Cardview都具有相同的标高设置,但是当我部署此布局时,它们具有不同的阴影,知道这可能是什么原因吗?

我切割了屏幕的一部分并将切口彼此相邻放置,以便您可以看到问题...... 在此输入图像描述

编辑

第一个项目的布局非常复杂,但该项目类型中的所有三个CardView都具有相同的阴影(上图中左侧的阴影)。第二种项目类型的根卡片视图具有图像右侧的阴影。

default_elevation = 3dp 
default_corner_radius = 2dp
margins are 16dp, 8dp, 4dp etc ...
Run Code Online (Sandbox Code Playgroud)

第一个项目类型:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:id="@+id/root"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_and_half"
            android:layout_marginLeft="@dimen/default_padding"/>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_quarter"
            android:layout_marginLeft="@dimen/margin_half"
            android:layout_marginRight="@dimen/margin_half"
            android:layout_marginBottom="@dimen/margin_half"
            app:cardCornerRadius="@dimen/default_corner_radius"
            app:cardElevation="@dimen/default_elevation"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/default_padding"
                android:paddingRight="@dimen/default_padding"
                android:paddingTop="@dimen/margin_and_half"
                android:paddingBottom="@dimen/margin_and_half"
                android:gravity="center_vertical">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="bottom"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:textSize="48sp"

                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:textSize="24sp"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/default_padding"
                    android:gravity="center">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingRight="@dimen/margin_half"
                        android:textColor="@color/green"
                        />

                    <ImageView
                        android:layout_width="@dimen/default_size"
                        android:layout_height="@dimen/default_size"/>

                </LinearLayout>

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/default_padding"
            android:layout_marginLeft="@dimen/default_padding"
            android:layout_marginBottom="@dimen/margin_quarter"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"/>

            <TextView
                android:layout_width="0dp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_quarter"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/margin_half"
            android:paddingRight="@dimen/margin_half"
            android:paddingBottom="@dimen/margin_default"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/margin_quarter"
                app:cardCornerRadius="@dimen/default_corner_radius"
                app:cardElevation="@dimen/default_elevation">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_double"
                        android:paddingBottom="@dimen/margin_double"
                        android:layout_centerInParent="true"/>

                </RelativeLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                app:cardCornerRadius="@dimen/default_corner_radius"
                app:cardElevation="@dimen/default_elevation">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="@dimen/margin_double"
                        android:paddingBottom="@dimen/margin_double"
                        android:layout_centerInParent="true"/>

                </RelativeLayout>

            </android.support.v7.widget.CardView>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_half"
            android:layout_marginLeft="@dimen/default_padding"
            android:layout_marginBottom="@dimen/margin_quarter"/>

    </LinearLayout>

</layout>
Run Code Online (Sandbox Code Playgroud)

第二项类型:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="@dimen/margin_half"
        android:layout_marginRight="@dimen/margin_half"
        android:layout_marginBottom="@dimen/margin_half"
        app:cardCornerRadius="@dimen/default_corner_radius"
        app:cardElevation="@dimen/default_elevation"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/default_padding">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="@dimen/margin_quarter"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>

                <TextView
                    android:id="@+id/textview2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="true"/>

            </LinearLayout>

            <TextView
                android:id="@+id/textview3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textAppearance="..(Too Long)"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</layout>
Run Code Online (Sandbox Code Playgroud)

小智 5

遇到了同样的问题,经过进一步调查后想将其留在这里:

在我看来,立面更注重给人一种深度感,而不仅仅是“简单地”投射阴影。因此,根据视图在屏幕上的位置,光照会有所不同,投射的阴影也会有所不同,而且阴影也取决于视图的轮廓。

关于这个问题,我推荐以下博客文章: https://blog.usejournal.com/playing-with-elevation-in-android-91af4f3be596

博客文章中提到的海拔测试仪应用程序也非常有用:https://play.google.com/store/apps/details ?id=me.seebrock3r.elevationtester&rdid=me.seebrock3r.elevationtester

但总而言之,我还无法创建基于标高的阴影,无论屏幕上的位置如何,该阴影对于每个视图都是一致的:(