一.使用属性
elevation
。
在Material Design主题当中给UI元素引入了高度的概念,视图的高度由属性Z来表示,决定了阴影的视觉效果,Z越大,阴影就越大且越柔和。但是Z值并不会影响视图的大小。
二.使用 layer-list
layer-list 的大致原理类似 RelativeLayout(或者FrameLayout) ,也是一层层的叠加 ,后添加的会覆盖先添加的。在 layer-list 中可以通过 控制后添加图层距离最底部图层的 左上右下的四个边距等属性,得到不同的显示效果。
我们可以通过 每一层通过设置
gradient来渐变颜色,显示阴影效果
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<
layer-list
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
>
<!-- 阴影部分 层级结构,每一层设置一边的阴影效果-->
<!-- gradient angle 180,必须是45的整数倍. 从右到左渐变 , right bottom 设置5dp,是预留 右边和底部阴影的宽度 -->
<
item
android
:left=
"0dp"
android
:top=
"0dp"
android
:right=
"5dp"
android
:bottom=
"5dp"
>
<
shape
android
:shape=
"rectangle"
>
<
gradient
android
:angle=
"180"
android
:endColor=
"#00000000"
android
:startColor=
"#ff000000"
/>
</
shape
>
</
item
>
<!-- gradient angle 270, 从上到下渐变 , left right 设置5dp,是预留 右边和左边阴影的宽度 , left="5dp"将左边阴影显示-->
<
item
android
:left=
"5dp"
android
:top=
"0dp"
android
:right=
"5dp"
android
:bottom=
"0dp"
>
<
shape
android
:shape=
"rectangle"
>
<
gradient
android
:angle=
"270"
android
:endColor=
"#00000000"
android
:startColor=
"#ff000000"
/>
</
shape
>
</
item
>
<!-- gradient angle 360, 从左到右渐变 , left bottom 设置5dp,是预留 底部和左边阴影的宽度 -->
<
item
android
:left=
"5dp"
android
:top=
"0dp"
android
:right=
"0dp"
android
:bottom=
"5dp"
>
<
shape
android
:shape=
"rectangle"
>
<
gradient
android
:angle=
"360"
android
:endColor=
"#00000000"
android
:startColor=
"#ff000000"
/>
</
shape
>
</
item
>
<!-- 背景部分 -->
<!-- 布局背景,将 左 右 底部的阴影显示出来 -->
<
item
android
:left=
"5dp"
android
:top=
"0dp"
android
:right=
"5dp"
android
:bottom=
"5dp"
>
<
shape
android
:shape=
"rectangle"
>
<
solid
android
:color=
"
@android:color/white
"
/>
</
shape
>
</
item
>
</
layer-list
>
三.最简单,找UI给切个带阴影的图片做背景,做成.9图片,控制子控件的显示位置。