Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在创建一个自定义TextView,我想知道是否可以在WRAP_CONTENT不复制和粘贴其父onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)实现的情况下修改生成的高度计算。
TextView
WRAP_CONTENT
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
假设我的WRAP_CONTENT计算等于父母的结果计算 + 16dp。
16dp
在您的自定义视图中,让 super 完成其工作,然后修改结果:
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) setMeasuredDimension(measuredWidth,measuredHeight + 16) }
(这里 16 是 16px,所以如果你想要的话,你必须为 16dp 进行转换。)
请参阅View#setMeasuredDimension。