Android ViewGroup: what should I do in the onLayout() override?

后端 未结 2 515
暖寄归人
暖寄归人 2020-12-24 13:24

When extending an Android ViewGroup class, what is the purpose of the onLayout() override? I\'m making a custom control in Android but for some reason the cont

2条回答
  •  眼角桃花
    2020-12-24 13:51

    In onLayout you need to call layout method on each child of this ViewGroup and provide desired position (relatively to parent) for them. You can check source code of FrameLayout (one of the simpliest subclasses of ViewGroup) to find out how it works.

    Although, if you don't need any "special" layouting, you have other options:

    • Extend some another subclass of ViewGroup instead (FrameLayout for example)
    • Use LayoutInflater if you just need your control to look exactly as in XML (which, I think, is exactly your case)

提交回复
热议问题