继承ConstraintLayout

开发中复杂的布局基本上都可以通过ConstraintLayout实现,所以我们继承ConstraintLayout实现一个EasyConstraintLayout能够为子view添加圆角和阴影效果。

public class EasyConstraintLayout extends ConstraintLayout {
public EasyConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public LinearLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
重写了两个方法,我们要用这些方法实现子view自定义属性的读取,在此之前要在xml中自定义一些属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--为了方便扩展其他layout,定义在外层,命名以layout_开头,否则lint会报红警告-->
<attr name="layout_radius" format="dimension" />
<attr name="layout_shadowColor" format="color" />
<attr name="layout_shadowEvaluation" format="dimension" />
<attr name="layout_shadowDx" format="dimension" />
<attr name="layout_shadowDy" format="dimension" />
<!--用统一一个EasyLayout,用于封装读取自定义属性-->
<declare-styleable name="EasyLayout">
<attr name="layout_radius" />
<attr name="layout_shadowColor" />
<attr name="layout_shadowEvaluation" />
<attr name="layout_shadowDx" />
<attr name="layout_shadowDy" />
</declare-styleable>
<!--和EasyLayout属性列表一样,但是命名要以XXX_Layout格式,这样开发工具会提示自定义属性-->
<declare-styleable name="EasyConstraintLayout_Layout">
<attr name="layout_radius" />
<attr name="layout_shadowColor" />
<attr name="layout_shadowEvaluation" />
<attr name="layout_shadowDx" />
<attr name="layout_shadowDy" />
</declare-styleable>
</resources>
--------------------- 

转载于:https://www.cnblogs.com/ly570/p/11284698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值