View自定义注意问题

1ViewGroup的onMeasure方法,测量的值应该是父容器的内边距加上所有子元素的测量宽高和外边距,自定义View时最好让view支持wrap_content,如果有必要,让你的view支持padding。

2 getMeasureWidth()方法在measure()过程结束后就可以获取到了,而getWidth()方法要在layout()过程结束后才能获取到。另外,getMeasureWidth()方法中的值是通过setMeasuredDimension()方法来进行设置的,而getWidth()方法中的值则是通过视图右边的坐标减去左边的坐标计算出来的。

3View的具体尺寸是通过其自身的测量规格(measureSpec计算出来的,而View的measureSpec是由父视图的measureSpec和view自身的LayoutParams共同决定的。

measure过程就是用来处理match_parent或者wrap_content,假如layout中规定所有View的layout_width和layout_height必须赋值成具体的数值,那么measure其实是没有必要的,但是加入match_parent或者wrap_content,会使得布局更加灵活。

5 mesure过程中几个关键方法:

  (1)public final void measue(int widthMeasureSpec, int heightMeasureSpec);

  (2)protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec);

  (3)protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec)

  (4)protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)

  (5)protected void measureChildWithMargins(View child,int parentWidthMeasureSpec, int widthUsed, int                parentHeightMeasureSpec, int heightUsed)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值