What you need to know about Android custom View

    1. Let View support wrap_content

       This is because the controls that directly inherit View or Viewgroup, if the onmesure does not do special processing for wrap_content, then when the outside world uses wrap_content in the layout, the expected effect will not be achieved.

    2. If necessary, make your View support padding

       This is because the control directly inherits the View. If the padding is not handled in the draw method, the padding property will not work. In addition, controls directly inherited from ViewGroup need to consider padding and the margin of sub-elements in onMeasure and onLayout, otherwise the padding and margin of sub-elements will be invalid.

    3. Try not to use Handler in View, it is not necessary

      This is because the View itself provides post series methods, which can completely replace the role of Handler.

    4. If there is a thread or animation in the View, it needs to be stopped in time.

       If there are threads or animations that need to stop, then onDetachedFromWindow is a good time. When the activity containing the secondary View exits or the current View is removed, the View's onDetachedFromWindow method will be called, which corresponds to onAttachedToWindow.

      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529033&siteId=291194637