【问题标题】:android:elevation only have shadow effects on the bottom side, how to make the shadow effects show on top side?android:elevation只有底部有阴影效果,如何让阴影效果显示在顶部?
【发布时间】:2015-02-28 18:00:00
【问题描述】:

我在一个activity的底部使用了一个framelayout,为了在fragment上显示阴影效果,我添加了android:elevation。但是阴影效果只出现在底部而不出现在顶部,任何人都可以给我一些建议吗?

<FrameLayout
    android:id="@+id/bottom_container"
    android:background="#00737f"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom"
    android:elevation="4dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"/>

【问题讨论】:

    标签: android material-design shadow android-framelayout android-elevation


    【解决方案1】:

    有一个技巧可以用来在视图上方显示阴影。

    基本上我们必须使用两个嵌套布局,其中外部布局使用elevation 投射阴影,内部布局设置background。 然后通过将padding设置为外部布局,我们可以将内部布局向下移动,而不移动阴影,从而使更多的阴影变得可见:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:elevation="4dp"
        android:outlineProvider="bounds"
        android:paddingTop="2dp"
        android:layout_marginTop="-2dp">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00737f">
    
            <!-- content -->
    
        </FrameLayout>
    
    </FrameLayout>
    

    这里有一个重要的属性outlineProvider,即使没有设置背景,它也可以使外部布局投射阴影。

    我们进一步指定一个负数margin 来补偿填充造成的偏移。根据用例,我们可以省略它。

    但是注意:如果我们将 View 移动太多,一些渲染伪影就会变得可见:

    Source of this example on Github

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 1970-01-01
      • 2021-02-24
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多