使用android:elevation在LinearLayout顶部投射阴影 [英] Cast shadow on top of LinearLayout using android:elevation
问题描述
我有这个LinearLayout
,它将被放置在活动布局的底部.我希望此LinearLayout
具有4dp高程,就像顶部工具栏应该具有的那样,但是,由于android:elevation
将阴影放置在ui组件下方,并且此特定组件(linearLayout)将位于屏幕底部,我根本看不到任何海拔.
I have this LinearLayout
that is going to be placed on the bottom of an activity layout. I want this LinearLayout
to have a 4dp elevation, just like the top toolbar should have, however, since android:elevation
places the shadow below the ui component and this specific component (linearLayout) is going to be on the bottom of the screen, I won't see any elevation at all..
这是我的LinearLayout
代码,并带有实现默认高程的图像:
This is my LinearLayout
code, and an image of it with the default elevation implemented:
<?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>
是否可以使用elevation
在ui组件顶部放置阴影?
预先感谢!
Is there a way, using elevation
to place a shadow on top of the ui component?
Thanks in advance!
推荐答案
从理论上讲,您无法选择android:elevation
进行此操作,因为您无法选择要投射阴影的方向.
You can't theoretically do it with android:elevation
, in the sense that you can't choose the direction where the shadow is going to be cast.
有两种解决方案.
1.可绘制对象
例如,您可以在布局的正上方放置ImageView
并设置android:src="@drawable/shadow"
.这应该是XML中定义的垂直GradientDrawable
,如
You could, for instance, put an ImageView
right above your layout and set android:src="@drawable/shadow"
. This should be a vertical GradientDrawable
defined in XML, as explained here.
2.解决方法
虽然大多数阴影实际上在视图下方,但微妙的阴影也在上方.一种解决方法可能是对elevation
使用非常高的值,例如40dp
:由于布局的原因,底部将被隐藏,而顶部将被扩展并看起来像一个普通的阴影.
While most of the shadow is actually below the view, a subtle shadow is also above. A workaround might be using a very high value for elevation
, like 40dp
: the bottom part is going to be hidden due to your layout, while the top is going to be expanded and look like a common shadow.
在任何一种情况下,您都无法控制dp
中的高程值,因为您无法确定阴影是否等同于android:elevation=4dp
投射的阴影.
In either case, you do not have control over the elevation value in dp
, in the sense that you can't be sure your shadow is equivalent to the one cast by android:elevation=4dp
.
这篇关于使用android:elevation在LinearLayout顶部投射阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!