我如何从Java代码动态设置Android中按钮的layout_weight
属性值?
我如何从Java代码动态设置Android中按钮的layout_weight
属性值?
以下是 Kotlin 的实现方法:
val params = mButton.layoutParams as LinearLayout.LayoutParams
params.weight = 1.0f
mButton.layoutParams = params
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
); YOUR_VIEW.setLayoutParams(param);
LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
然后分配“weight”参数所需的值。
linearLayoutParams.weight = 5;
button.requestLayout();