两种视图并排放在一起。

时间:2023-01-21 11:54:46

I have two views with the same elevation beside each other. My wanted behaviour is that they won't cast a shadow over each other as they have the same elevation, however, what is happening is that the view on the left, casts a shadow on the right. They are not the same size so I can't put them both in another view and apply an elevation to that view.

我有两种观点,在同一高度的旁边。我想要的行为是,他们不会给彼此投下阴影,因为他们有同样的高度,然而,正在发生的是,左边的观点,投下阴影在右边。它们的大小不同,所以我不能把它们都放到另一个视图中并对那个视图应用一个高程。

Is this the expected behaviour? Is there a way round it?

这是预期的行为吗?有办法绕过它吗?

Edit:

编辑:

I just recreated with simpler views, here is the code. I also noticed it has the expected behaviour if I have the view directly in the layout and don't include it as I did it in this example and as I need it to work.

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

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

And here is the include:

这里包括:

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

And the screenshot:

截图:

两种视图并排放在一起。

1 个解决方案

#1


1  

See the hierarchy you have:

看看你的等级制度:

两种视图并排放在一起。

So you have applied elevation to 1 and 3, which are not siblings. Apparently, if one view is higher in the hierarchy, than it should cast a shadow, regardless those views have same elevation or no.

所以你把高程应用到1和3,这不是兄弟姐妹。显然,如果一个视图在层次结构中是较高的,那么它就不应该投下阴影,不管这些视图是否具有相同的高度。

Had you applied elevation to 2 instead of 3 you would not see shadow effect.

如果你把仰角改为2,而不是3,你就看不到阴影效果了。

So if you just change your test.xml to this:

如果你改变测试。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>

You'd get this output:

你会得到这个输出:

两种视图并排放在一起。

#1


1  

See the hierarchy you have:

看看你的等级制度:

两种视图并排放在一起。

So you have applied elevation to 1 and 3, which are not siblings. Apparently, if one view is higher in the hierarchy, than it should cast a shadow, regardless those views have same elevation or no.

所以你把高程应用到1和3,这不是兄弟姐妹。显然,如果一个视图在层次结构中是较高的,那么它就不应该投下阴影,不管这些视图是否具有相同的高度。

Had you applied elevation to 2 instead of 3 you would not see shadow effect.

如果你把仰角改为2,而不是3,你就看不到阴影效果了。

So if you just change your test.xml to this:

如果你改变测试。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>

You'd get this output:

你会得到这个输出:

两种视图并排放在一起。