同高度的两个视图并排显示

14
我有两个高度相同的视图并排放置。我希望它们不会互相投射阴影,因为它们具有相同的高度。但实际情况是左边的视图会在右边投射阴影。它们大小不同,所以我不能将它们放在另一个视图中并给该视图应用高度。
这是预期的行为吗?有没有解决方法?
编辑:
我使用更简单的视图重新创建了一下,以下是代码。 我还注意到,如果我将视图直接放在布局中,而不像我在这个示例中那样包含它,则它具有预期的行为,正如我需要的那样。
<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>

而且这里有截图:


您可以尝试截取屏幕截图,将其上传至某处,并在问题中附上链接及使用的布局。 - CommonsWare
抱歉耽搁了,我刚刚使用一个更简单的视图重新创建了这个问题。如果我不包含这个视图,它是可以工作的,但这正是我在原始问题中使用该视图的方式。 - Daniel Julio
2
如果您说<include>是您的问题,那么我怀疑解决方案是避免使用<include>。例如,也许您可以创建一个自定义的View/ViewGroup,其中包含您的两个视图。 - CommonsWare
是的,那可能是前进的方式。我以为<include>不会影响视图的高度,但它似乎完全删除了它。 - Daniel Julio
当使用FrameLayoutRelativeLayout而不是LinearLayout时,会有什么行为?我认为阴影的表现会因此而异。嵌套和子布局也可能会影响阴影。 - Abtin Gramian
1个回答

5

看一下你的层次结构:

enter image description here

因此,你已经给不是兄弟姐妹的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>

您将得到以下输出:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接