我需要获取已通过编程方式创建但未调用RelativeLayout
的{{1}}的高度。
我尝试过的事情:
setContentView()
onGlobalLayout
代码:
View.post()
现在,如果我要求val relativeLayout = RelativeLayout(activity)
val rlp = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
rlp.marginStart = 12
rlp.marginEnd = 12
val tv = TextView(activity)
tv.text = text
val lp = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
lp.addRule(RelativeLayout.CENTER_IN_PARENT)
lp.marginEnd = 8
lp.marginStart = 8
tv.setLayoutParams(lp)
relativeLayout.addView(tv)
的高度,那么如果我使用setContentView并再次询问它是否有效,它当然是0。
有什么办法吗?
修改后的代码:
relativeLayout