我试图在MainActivity.kt中创建新的LinearLayout
for (i in 0 until tileArr.size){
var tileLayout: LinearLayout = LinearLayout(this)
tileLayout.marginBottom = 10
}
抛出错误Val不能重新赋值在线:tileLayout.marginBottom = 10
您不能直接修改这些属性,您需要使用LayoutParams
。
for (i in 0 until tileArr.size){
var tileLayout: ViewGroup = LinearLayout(this)
val params = <Parent ViewGroup Type>.LayoutParams( // if the parent is FrameLayout, this should be FrameLayout.LayoutParams
LinearLayout.LayoutParams.WRAP_CONTENT, // modify this if its not wrap_content
LinearLayout.LayoutParams.WRAP_CONTENT // modify this if its not wrap_content
)
params.setMargins(0, 0, 0, 10) // last argument here is the bottom margin
tileLayout.layoutParams = params
}