如何从代码动态设置layout_weight属性?
如何从 java 代码动态设置 android 中按钮的属性 layout_weight
值?
How can I set the value for the attribute layout_weight
for button in android dynamically from java code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以将其作为 LinearLayout.LayoutParams 构造函数的一部分传递:
最后一个参数是权重。
You can pass it in as part of the
LinearLayout.LayoutParams
constructor:The last parameter is the weight.
使用
LinearLayout.LayoutParams
:编辑:啊,Erich 的回答更简单!
Use
LinearLayout.LayoutParams
:EDIT: Ah, Erich's answer is easier!
如果您已经在布局(xml)文件中定义了视图,只想以编程方式更改权重,这种方式更好,
新的 LayoutParams 会覆盖 xml 文件中定义的其他参数(例如边距),或者您需要在 LayoutParams 中指定所有参数。
If you already define your view in your layout(xml) file, only want to change the weight programmatically, this way is better
new a LayoutParams overwrites other params defined in you xml file like margins, or you need to specify all of them in LayoutParams.
如果具有宽度、高度和重量的构造函数不起作用,请尝试使用具有宽度和高度的构造函数。然后手动设置重量。
如果希望根据权重设置宽度,请在构造函数中将宽度设置为 0。同样适用于身高。
下面的代码对我有用。
If the constructor with width, height and weight is not working, try using the constructor with width and height. And then manually set the weight.
And if you want the width to be set according to the weight, set width as 0 in the constructor. Same applies for height.
Below code works for me.
如果
layoutparams
已经定义(在 XML 中或动态定义),下面是一个行:If
layoutparams
is already defined (in XML or dynamically), Here's a one liner:如果我有人寻找答案,请使用这个:
如果您从 xml 文件初始化布局,这将比为线性布局提供新的布局参数方便得多。
If I someone looking for answer, use this:
If you are initializing your layout from xml file, this will be much more convenient than providing new layout parameters for Linear Layout.
LinearLayout.LayoutParams
和TableLayout.LayoutParams
中的任何一个都对我有用,对于按钮,正确的一个是TableRow.LayoutParams
。即:关于使用
MATCH_PARENT
或WRAP_CONTENT
是相同的。Any of
LinearLayout.LayoutParams
andTableLayout.LayoutParams
worked for me, for buttons the right one isTableRow.LayoutParams
. That is:About using
MATCH_PARENT
orWRAP_CONTENT
be the same.如果您已经在layout(xml) 文件中定义了视图,并且只想在语法上更改权重程序,那么创建新的LayoutParams 会覆盖您在xml 文件中定义的其他参数。
所以首先你应该使用“getLayoutParams”,然后setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);
If you have already defined your view in layout(xml) file and only want to change the weight pro grammatically, then then creating new LayoutParams overwrites other params defined in you xml file.
So first you should use "getLayoutParams" and then setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);
我通过添加这行代码完成了我的任务!
I have done my task by adding this line of code!
使用 Kotlin 你可以做类似的事情:
Using Kotlin you can do something like:
如果您想将权重设置为 LinearLayout ,请使用下面提到的代码或将权重设置为relative layout,然后将其替换为relative layout
);
YOUR_VIEW.setLayoutParams(param);
if you want to set weight to LinearLayout then use code mentioned below or set weight to Relative Layout then replace it with Relative Layout
);
YOUR_VIEW.setLayoutParams(param);
你可以尝试这个方法。
首先,您获取与此 Button 关联的 LayoutParams。执行类型转换。
然后为“权重”参数分配所需的值。
重新绘制布局。
You can try this approach.
First, you get the LayoutParams associated with this Button. Perform a type conversion.
Then assign the required value of the "weight" parameter.
Redraw the layout.
在 Kotlin 中您可以这样做:
This is how you do it in Kotlin: