使用android:elevation在LinearLayout上投射阴影

yat*_*at0 28 android android-layout material-design android-5.0-lollipop

我有这个LinearLayout将被放置在活动布局的底部.我希望它LinearLayout具有4dp高程,就像顶部工具栏应该具有的那样,但是,因为android:elevation将阴影置于ui组件下方并且此特定组件(linearLayout)将位于屏幕的底部,我将看不到任何高度......

这是我的LinearLayout代码,以及实现默认高程的图像:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:background="@android:color/transparent"
    android:orientation="horizontal"
    android:elevation="4dp"
    android:layout_alignParentBottom="true">

    <ImageButton
        android:id="@+id/playButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="center"
        android:clickable="true"
        android:background="@drawable/bottom_toolbar_menu_selector"
        android:src="@drawable/ic_play"
        style="?android:attr/borderlessButtonStyle" />

    <ImageButton
        android:id="@+id/stopButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="center"
        android:clickable="true"
        android:background="@drawable/bottom_toolbar_menu_selector"
        android:src="@drawable/ic_stop"
        style="?android:attr/borderlessButtonStyle" />

    <ImageButton
        android:id="@+id/bookmarkButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="center"
        android:clickable="true"
        android:background="@drawable/bottom_toolbar_menu_selector"
        android:src="@drawable/ic_bookmark"
        style="?android:attr/borderlessButtonStyle" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有没有办法,使用elevation阴影放在ui组件的顶部? 提前致谢!

nat*_*rio 33

android:elevation从某种意义上说,你无法选择阴影将要投射的方向,理论上你无法做到这一点.

有两种解决方案.

1. Drawables

例如,你可以放一个 ImageView在您的布局上方右侧并设置android:src="@drawable/shadow".这应该是一个垂直GradientDrawable于XML定义,如解释在这里.

2.解决方法

虽然大部分阴影实际上位于视图下方,但上方还有一个微妙的阴影.解决方法可能使用非常高的值elevation,例如40dp:由于您的布局,底部将被隐藏,而顶部将被扩展并看起来像一个普通的阴影.

在任何一种情况下,您都无法控制高程值 dp,因为你不能确定你的阴影等同于投射的阴影android:elevation=4dp.

  • 解决方案解决方案非常适合我的需求!谢谢 (2认同)