如何从代码中动态设置layout_weight属性?

我如何从java代码动态设置属性layout_weight按钮在android的值?

257138 次浏览

你可以把它作为LinearLayout.LayoutParams构造函数的一部分传入:

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);

最后一个参数是权重。

使用LinearLayout.LayoutParams:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);

编辑:啊,埃里克的答案更简单!

LinearLayout.LayoutParamsTableLayout.LayoutParams中的任何一个都适合我,对于按钮,右边的是TableRow.LayoutParams。那就是:

            TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f);

关于使用MATCH_PARENTWRAP_CONTENT是相同的。

如果带有width、height和weight的构造函数不起作用,请尝试使用带有width和height的构造函数。然后手动设置权重。

如果你想根据权重设置宽度,在构造函数中将width设置为0。身高也是一样。

LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);


LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam2.weight = 0.7f;
child2.setLayoutParams(childParam2);


parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);

如果你已经在你的layout(xml)文件中定义了你的视图,只想通过编程改变权重,这种方法更好

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);

new a LayoutParams会覆盖xml文件中定义的其他参数,比如页边距,或者你需要在LayoutParams中指定所有的参数。

如果我有人在寻找答案,用这个:

LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;

如果你从xml文件初始化布局,这将比为线性布局提供新的布局参数方便得多。

如果你在语法上有已经在layout(xml)文件中定义了视图,只想更改权重,那么创建新的LayoutParams会覆盖xml文件中定义的其他参数。

所以首先你应该使用"getLayoutParams"然后setLayoutParams

< p > <代码> LinearLayout。LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams(); 参数个数。重量= 4f; mButton.setLayoutParams (params); < /代码> < / p >

如果layoutparams已经定义(以XML或动态方式),这里有一行代码:

((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;

使用Kotlin你可以做以下事情:

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.widget.*


import android.widget.LinearLayout


class RespondTo : CardView {
constructor(context: Context) : super(context) {
init(context)
}


private fun init(context: Context) {




val parent = LinearLayout(context)


parent.apply {
layoutParams = LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, 1.0f).apply {
orientation = LinearLayout.HORIZONTAL


addView(EditText(context).apply {
id = generateViewId()
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.9f).apply {
}
})
addView(ImageButton(context).apply({
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.1f)
background = null
setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
id = generateViewId()
layoutParams = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// addRule(RelativeLayout.LEFT_OF, myImageButton.id)
}
}))
}
}
this.addView(parent)
}
}

我已经通过添加这行代码完成了我的任务!

LinearLayout.LayoutParams mylayout = (LinearLayout.LayoutParams)
myview.getLayoutParams();
mylayout.weight = 2;
myview.setLayoutParams(mylayout);