首页 > 学院 > 开发设计 > 正文

在控件下方添加阴影

2019-11-09 14:18:18
字体:
来源:转载
供稿:网友

在控件下方添加阴影

最近在开发过程中,想要实现一种效果,效果如图(图来源stackoverflow):

img

在tablayout下添加一道阴影,由于一些特殊的原因不能使用elevation属性来实现相应的阴影效果。于是上网搜索如何制作该种效果。

制作相应的背景图片

其实连制作都不需要我们自己动手,因为早有谷歌帮助了我们制作了相应的图片,放上链接

https://github.com/google/iosched/blob/dfaf8b83ad1b3e7c8d1af0b08d59caf4223e0b95/android/src/main/res/drawable-xxhdpi/bottom_shadow.9.png

然后我们在相应的控件上相应的属性,这里建议使用foreground属性,因为如若你的控件内还有其他子控件,使用foreground就不会将阴影画在他们身上(如果你需要这种效果,那当我没说)。

xml

有图片的方法,当然也有我们自己制作相应的xml了,首先创建一个drawable名为 shadow.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="@android:color/transparent" android:endColor="#33000000" android:angle="90"> </gradient></shape>

然后将阴影添加到你的内容结构中,但不是你想的那样设置在某个属性中,应该 像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 你的View放在这里 --> <View android:layout_width="match_parent" android:layout_height="8dp" android:background="@drawable/shadow" /></RelativeLayout>

两种方法兼容性也是杠杠的,大家自由选择。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表