【问题标题】:Two views with same elevation side-by-side并排具有相同高度的两个视图
【发布时间】:2015-08-24 11:18:31
【问题描述】:

我有两个彼此相邻的具有相同高度的视图。我想要的行为是它们不会相互投射阴影,因为它们具有相同的高度,但是,正在发生的事情是左侧的视图在右侧投射了阴影。它们的大小不一样,所以我不能将它们都放在另一个视图中并将高程应用到该视图。

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

编辑:

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

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

还有截图:

【问题讨论】:

  • 您可能希望截取您看到的效果的屏幕截图,将其上传到某处,并从您的问题中链接到它,同时提供您正在使用的布局。
  • 抱歉耽搁了,我只是使用更简单的视图重新创建了问题。如果我不包含视图,它会起作用,但这就是我在原始问题中使用视图的方式。
  • 如果您说&lt;include&gt; 是您的问题,那么我怀疑解决方案是避免使用&lt;include&gt;。例如,也许您可​​以创建一个自定义的View/ViewGroup,它具有您的两个视图。
  • 是的,这可能是前进的方向。我认为 不会影响视图的高度,但似乎它完全删除了它。
  • 使用FrameLayoutRelativeLayout 等其他布局而不是LinearLayout 时的行为是什么?我认为阴影的行为基于此不同。嵌套和子布局也可能会影响阴影。

标签: android android-layout android-view android-elevation


【解决方案1】:

查看您的层次结构:

所以您已将提升应用到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>

你会得到这个输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2017-10-25
    • 2016-05-13
    • 1970-01-01
    • 2019-01-13
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多