在自定义布局中定位充气的视图

我写了一个继承自RelativeLayout的自定义布局。

它有一个方法(在布局被膨胀后调用),当被调用时会膨胀一个视图,将其附加到布局,然后将其与布局相关联。

但是,当我尝试设置布局参数时,它似乎什么都不做:

fun createCell() { // Inflate a cell and attach it to this val cell = inflate(context, eventCellId, this) // Setup layout params for the cell val params = RelativeLayout.LayoutParams(400, 400) // Lets force it to be 200 from the top params.topMargin = 200 // Set the cell layout params cell.layoutParams = params } 

我也试过这个:

 fun createCell() { // Inflate a cell and attach it to this val cell = inflate(context, eventCellId, this) cell.top = 200 cell.bottom = 400 } 

单元格正在渲染,它只是不在指定的位置渲染。 我是否需要重写onMeasure或其他东西(即使在添加这些单元格之前,我的布局已经膨胀了)。 单元格在运行时动态添加。