android - 使用android :elevation在LinearLayout顶部转换阴影

标签 android android-layout android-5.0-lollipop material-design

我有这个 LinearLayout 将被放置在 Activity 布局的底部。我希望这个 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>

enter image description here

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

最佳答案

理论上你不能用 android:elevation 做到这一点,因为你不能选择转换阴影的方向。

有两种解决方案。

1.可绘制对象

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

<强>2。解决方法

虽然大部分阴影实际上位于 View 下方,但也有一个细微的阴影位于上方。一种解决方法可能是为 elevation 使用非常高的值,例如 40dp:由于您的布局,底部将被隐藏,而顶部将被展开并且看起来像一个普通的影子。

在任何一种情况下,您都无法控制 dp 中的高程值,因为您无法确定您的阴影是否等同于 android 转换的阴影:海拔=4dp.

关于android - 使用android :elevation在LinearLayout顶部转换阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32395983/

相关文章:

android - 将 jetpack compose 添加到现有项目

android - 升级到 SDK 21 - 膨胀类 android.support.v7.internal.widget.ActionBarContainer 时出错

android - Card_view Material 设计

android - RecyclerView clipToPadding = false

Android BroadcastReceiver 与服务

android - 以任何方式将 DroidCam 视频流式传输到 OpenCV Python

java - 单击按钮将回收器 View 滚动到特定位置

java - MultiPartEntity 以及 android 中的纯文本

java - 带有 Android 应用程序的多频段应用程序

android - 从浏览器打开我的 Android 应用程序的链接