android - 如何从代码中动态设置layout_weight属性?
如何从java代码动态设置android中的按钮属性layout_weight的值?
9个解决方案
377 votes
您可以将其作为LinearLayout.LayoutParams构造函数的一部分传递:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
最后一个参数是重量。
Erich Douglass answered -03-16T13:48:22Z
108 votes
使用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);
编辑:啊,Erich的答案更容易!
kcoppock answered -03-16T13:48:54Z
74 votes
如果您已经在layout(xml)文件中定义了视图,只想以编程方式更改权重,这种方式更好
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);
new a LayoutParams会覆盖您在xml文件中定义的其他参数,如margin,或者您需要在LayoutParams中指定所有这些参数。
pkliang answered -03-16T13:49:28Z
19 votes
如果宽度,高度和重量的构造函数不起作用,请尝试使用宽度和高度的构造函数。 然后手动设置重量。
如果要根据权重设置宽度,请在构造函数中将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);
childParam1.weight = 0.7f;
child2.setLayoutParams(childParam2);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);
Ganindu answered -03-16T13:50:02Z
12 votes
如果我有人在寻找答案,请使用:
LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;
如果要从xml文件初始化布局,这比为线性布局提供新的布局参数要方便得多。
ash1103 answered -03-16T13:50:35Z
6 votes
任何MATCH_PARENT和WRAP_CONTENT为我工作,对于按钮右边的是TableRow.LayoutParams.这是:
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
关于使用MATCH_PARENT或WRAP_CONTENT是一样的。
gantzer89 answered -03-16T13:51:10Z
2 votes
如果您已经在布局(xml)文件中定义了视图并且只想以编程方式更改权重,那么创建新的LayoutParams将覆盖您在xml文件中定义的其他参数。
首先你应该使用“getLayoutParams”然后使用setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);
Shirish Herwade answered -03-16T13:51:51Z
1 votes
如果已经定义了layoutparams(以XML或动态方式),这是一个单行:
((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;
SolidSnake answered -03-16T13:52:20Z
0 votes
使用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)
}
}
Hasan A Yousef answered -03-16T13:52:49Z