我正在学习一个自定义组件,我发现 onMeasure()
方法的 view
和 viewgroup
有一些不同。例如,有人调用super.onMeasure
,有人在最后使用setMeasuredDimension
。区别在哪里? measureChildren()
意味着什么?
第一次来这个社区,希望得到我想要的答案,谢谢。
最佳答案
super.onMeasure()
当您希望将自定义组件的测量留给您正在扩展的父类(super class)时使用。例如,假设您正在为多色文本构建自定义 TextView。您真的不想覆盖 TextView 的 onMeasure()
实现,因为您没有处理文本的宽度和高度。因此,您可以调用 super.onMeasure()
并将测量留给 TextView
。
setMeasuredDimension()
在这种情况下,您确实必须覆盖 onMeasure()
,您可以使用 setMeasuredDimension()
告诉 android View 渲染系统您自定义的最终计算宽度和高度看法。 View 系统将使用它来呈现您的 View 。
measureChildren() It tells the children of this
ViewGroup
to measure themselves using the givenMeasureSpec
.
非常好的讨论 here .
关于android - onMeasure() 方法的 View 和 View 组之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38197862/