作者:我想飞 | 来源:互联网 | 2023-10-11 04:45
Java
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.TOP;
button.setLayoutParams(params);
Kotlin
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
weight = 1.0f
gravity = Gravity.TOP
}
有关重力值以及如何设置重力,请检查“重力”。
基本上,您应该选择LayoutParams
依赖于父项。可以是RelativeLayout,LinearLayout
等等。