并排具有相同标高的两个视图

Two views with same elevation side-by-side

我有两个高度相同的视图并排放置。我想要的行为是它们不会相互投射阴影,因为它们具有相同的高度,但是,发生的是左侧的视图在右侧投射阴影。它们的大小不同,所以我不能将它们都放在另一个视图中并向该视图应用海拔高度。

这是预期的行为吗?有办法解决吗?

编辑:

我只是用更简单的视图重新创建,这是代码。 我还注意到,如果我直接在布局中拥有视图并且没有像我在本例中那样包含它并且因为我需要它工作,它会具有预期的行为。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@android:color/holo_green_dark">

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@android:color/holo_red_dark"
        android:elevation="24dp"/>

    <include layout="@layout/test"/>

</LinearLayout>

这是包括:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_dark"
        android:elevation="24dp"/>

</LinearLayout>

以及截图:

查看您的层次结构:

因此您已将提升应用到 13,它们不是兄弟姐妹。显然,如果一个视图在层次结构中更高,那么它应该投射阴影,无论这些视图是否具有相同的高度。

如果您将海拔应用到 2 而不是 3,您将不会 看到阴影效果。

因此,如果您只是将 test.xml 更改为:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="24dp">

    <LinearLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/holo_red_dark"/>

</LinearLayout>

你会得到这个输出: