如何在不复制和粘贴其 parent onMeasure 实现的情况下修改自定义 TextView WRAP_CONTENT 高度?

How to modify a custom TextView WRAP_CONTENT height without copying and pasting its parent onMeasure implementation?

我正在创建自定义 TextView,我想知道是否可以在不复制和粘贴其 parent onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)实施。

假设我的 WRAP_CONTENT 计算等于 parent 的计算结果 + 16dp.

在您的自定义视图中,让超级执行其工作然后修改结果:

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    setMeasuredDimension(measuredWidth,measuredHeight + 16)
}

(这里的16是16px,所以如果你想要16dp的话,你必须做16dp的转换。)

参见View#setMeasuredDimension