在XML中为layout设置阴影
1.写在前面因为有这个需求,所以搜很多资料,也看到了很多方法为layout添加阴影但是最好的方法我感觉是如下两个 2.干货<declare-styleable name="ShadowLayout">
<attr name="sl_cornerRadius" format="dimension"/>
<attr name="sl_shadowRadius" format="dimension"/>
<attr name="sl_shadowColor" format="color"/>
<attr name="sl_dx" format="dimension"/>
<attr name="sl_dy" format="dimension"/>
</declare-styleable>
这个库的自定义属性就这几个,一个个来看 <RelativeLayout android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="80dp" >
</RelativeLayout>
现在为这个layout添加阴影,代码如下 <com.dd.ShadowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="80dp"
>
</RelativeLayout>
</com.dd.ShadowLayout>
运行后如下 现在一个个设置属性,为这个控件添加如下属性 app:sl_shadowColor="@color/colorPrimary"
这个其实很明显 是个设置阴影的颜色。至于我添加的这个颜色就是生成项目自带的那个蓝色横条actionbar颜色。然后运行发现如下 app:sl_shadowColor="#803F51B5"
注意这里为什么将颜色抽出来,没有引用,因为 theme colorPrimary 不能使用带有alpha的颜色值,不然会报错。这不是重点。只要知道这个colorPrimary颜色就是#3F51B5就行了 这个测试完了,我将颜色修改成这个库使用指南中的颜色 测试第二个属性,在添加如下 app:sl_shadowRadius="4dp"
运行出来如下 app:sl_shadowRadius="14dp"
运行结果如下 将 app:sl_shadowRadius=”4dp”还原成4dp。并且添加如下两个属性 app:sl_dx="2dp"
app:sl_dy="2dp"
运行如下 app:sl_dy="2dp"
运行出来如图 sl_dx sl_dy 就是为布局右边和下边设置阴影偏移的属性。可能有人想问 要为上边或者左边设置阴影偏移怎么办 很简单 设置偏移量带上 - 就可以了 比如将代码修改如下 app:sl_dx="-6dp"
app:sl_cornerRadius="xxxdp"
·但是我试了很久还是看不出这个是什么意思。添加上去并没有效果 所以如果有知道可以说一声。最后再次感谢dai佬的这个库 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |