• 自定义ViewGroup


    ViewGroup

    --->A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

    ViewGroup的职责

    ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性,都是为用于告诉容器的),我们的宽度(layout_width)、高度(layout_height)、对齐方式(layout_gravity)等;当然还有margin等;于是乎,ViewGroup的职能为:给childView计算出建议的宽和高和测量模式 ;决定childView的位置;为什么只是建议的宽和高,而不是直接确定呢,别忘了childView宽和高可以设置为wrap_content,这样只有childView才能计算出自己的宽和高。

    coding原理

    View的根据ViewGroup传人的测量值和模式,对自己宽高进行确定(onMeasure中完成),然后在onDraw中完成对自己的绘制。ViewGroup需要给View传入view的测量值和模式(onMeasure中完成),而且对于此ViewGroup的父布局,自己也需要在onMeasure中完成对自己宽和高的确定。此外,需要在onLayout中完成对其childView的位置的指定。

    (1)在onMeasure中计算childView的测量值以及模式,以及设置自己的宽和高

    (2)onLayout对其所有childView进行定位(设置childView的绘制区域)

    代码待完善。。。

  • 相关阅读:
    pat 1029. Median (25)
    pat 1040. Longest Symmetric String (25)
    pat 1037. Magic Coupon (25)
    pat 1058. A+B in Hogwarts (20)
    pat 1083. List Grades (25)
    pat 1054. The Dominant Color (20)
    pat 1042. Shuffling Machine (20)
    pat 1061. Dating (20)
    *分支-11. 计算工资
    分支-10. 计算个人所得税
  • 原文地址:https://www.cnblogs.com/mbp-study/p/5884923.html
Copyright © 2020-2023  润新知