[英]Set LinearLayout weight in code rather than xml problem
I have the following code to try and set the weight of a linearLayout to 0.7, after looking at the documentation and some examples online it says it takes 3 parameters but in my code it will not let me: 在查看文档和一些在线示例后,我有以下代码尝试将linearLayout的权重设置为0.7,它说需要3个参数,但在我的代码中却不允许我这样做:
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
So here it sets both the height and the width but the last parameter it says in the documentation is the weight and it wont let me put it in there. 因此,这里它既设置了高度,又设置了宽度,但是文档中说的最后一个参数是重量,它不允许我放在其中。
Try this for the layoutparams for linearlayout: 尝试使用linearlayout的layoutparams:
linear.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,
1)
);
As a proof-of-concept: this works. 作为一个概念验证:这是有效的。 check out the import and compare them with your own :) 检查导入并将它们与您自己的:)进行比较
file: Test.java file:Test.java
package com.huiges.stackOverFlow.examples
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,
1)
);
}
}
Use LinearLayout.LayoutParams
使用LinearLayout.LayoutParams
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,1));
The last parameter takes a float. 最后一个参数采用浮点数。 0.7
is a double, use 0.7f
instead to get a float. 0.7
是双0.7f
,请改用0.7f
获得浮点数。
linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT,
0.7f));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.