您的位置:首页 > 移动开发 > Android开发

Android ViewGroup使用小结

2016-01-09 11:53 375 查看

ViewGroup定义

在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我们能够明确。在ViewGroup中能够包括其它UI控件。包括我们的自己定义控件

优势及用处

我们了解UI控件最大的目的是使用它。而要了解控件。必须明确该控件的长处。

ViewGroup在Android的UI控件中相对复杂,它的自己定义程度非常高。借助declare-styleable自己定义控件属性,差点儿控件的各个方便都能够控制到。

了解到这些长处之后,你会想到哪里有ViewGroup的用武之地呢?考虑考虑,我会在文章最后说说我的理解。

绘制流程

ViewGroup中有两个及其重要的回调方法

onMeasure(int widthMeasureSpec, int heightMeasureSpec)

onLayout(boolean changed, int left, int top, int right, int bottom)

onMeasure

Ask all children to measure themselves and compute the measurement of this layout based on the children.

onLayout

Position all children within this layout.

以上是api中对两个方法的解释。真实使用中onMeasure和onLayout都可能被调用多次。依据log我们会发现onLayout都是在onMeasure之后才调用,这也证实了api的说法,ViewGroup在画图之前先把数值算好,然后依据相应的值把控件绘制到相相应的地方。

getMeasuredWidth

这种方法在这里单独拿出来说一下。在onMeasure方法中计算控件存放位置时。我们会用到getMeasuredWidth()和getMeasuredHeight(),这里得到的值包含padding,不包含margin,在计算的时候要注意
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: