zoukankan      html  css  js  c++  java
  • Android ViewGroup使用小结

    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,在计算的时候要注意

  • 相关阅读:
    初试django
    初试mysql
    ASP.NET MVC 做的网站项目
    Update 更新语句使用两个表关联
    SQL Server 输出所有表结构
    循环数据集字段赋默认值
    FireBird.conf配置文件常用参数
    Delphi 获取临时数据集 ClientDataSet
    DELPHI 读取csv 格式文本文件
    获取 临时数据集 的两种方法
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5115811.html
Copyright © 2011-2022 走看看