Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式更改布局权重_Java_Android - Fatal编程技术网

Java 以编程方式更改布局权重

Java 以编程方式更改布局权重,java,android,Java,Android,我试图以编程方式将视图的布局权重设置为随机数。我认为我的方法是正确的,但我无法获得一些技术细节。这就是我到目前为止所做的: public class EnterText extends Activity { View view = findViewById(R.id.view1); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns

我试图以编程方式将
视图的布局权重设置为随机数。我认为我的方法是正确的,但我无法获得一些技术细节。这就是我到目前为止所做的:

public class EnterText extends Activity {

    View view = findViewById(R.id.view1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_text);

        Random r = new Random();
        int i1 = r.nextInt(5 - 1) + 1;

        //This is to put the weight in a format like "1f"
        String weight = new StringBuilder(String.valueOf(i1)).append("f").toString();

        int height1;
        view.getLayoutParams().height = height1;    

        int width1;
        view.getLayoutParams().height = width1; 

        //Set the weight
        view.setLayoutParams(new LayoutParams(LayoutParams.height1, LayoutParams.width1, weight));

    }

}

我相信这是接近工作,但我遇到了一些错误。首先,我得到了错误
,类型EnterText的方法StringBuilder(String)未定义,我认为不应该发生这种情况。其次,在
0dip
,我得到了令牌“.0d”上的错误
语法错误。应为
。是否有人知道如何修复此问题,是否有人知道如何更好地执行我正在尝试的操作?

在setContentView调用后,将引用添加到该视图中

    setContentView(R.layout.activity_enter_text);
    view = findViewById(R.id.view1);
对于EnterText类型的
StringBuilder(String)未定义,请使用
new
关键字创建新的StringBuilder(我认为您不需要此选项)

对于令牌“.0d”上的
语法错误。应为
,只需使用getLayoutParams().weight并为其指定一个浮点值,而不是int值

LinearLayout.LayoutParams lllp = (LinearLayout.LayoutParams) view.getLayoutParams();
lllp.weight = 0.25f
view.setLayoutParams(lllp); // this one may not be needed
应该看起来像:

View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_text);
    view = findViewById(R.id.view1);


    //Set the weight using a float n

    LinearLayout.LayoutParams lllp = (LinearLayout.LayoutParams) view.getLayoutParams();
    lllp.weight = 0.25f; // supply a float like this
    int height1 = 200; // you have to supply a value
    int width1 = 300;  // you have to supply a value
    lllp.width = width1;
    lllp.height = height1; 
    view.setLayoutParams(lllp); // this one may not be needed

}

请看我的更新代码,当我尝试你的第二个建议时,我得到了一个不同的错误。但是你的第一个有效!当我尝试此操作时,会出现以下错误:
height1无法解析或不是字段
width1无法解析或不是字段
您确定更新的答案有效吗?现在它表示,
未使用局部变量权重的值
sorry..正在根据您的编辑进行编辑。。查看我的新editCool,但是您的代码是否具有我的代码中的随机元素?或者我把它插入中间?
View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_text);
    view = findViewById(R.id.view1);


    //Set the weight using a float n

    LinearLayout.LayoutParams lllp = (LinearLayout.LayoutParams) view.getLayoutParams();
    lllp.weight = 0.25f; // supply a float like this
    int height1 = 200; // you have to supply a value
    int width1 = 300;  // you have to supply a value
    lllp.width = width1;
    lllp.height = height1; 
    view.setLayoutParams(lllp); // this one may not be needed

}