简体   繁体   English

将LinearLayout放入LinearLayout数组

[英]Putting LinearLayout to LinearLayout Array

i wanna create button 1 to 9 and i want to do that in loop. 我想创建按钮1到9,我想循环执行。 But in each 3 count, i want to create a new LinearLayout. 但是在每3个计数中,我想创建一个新的LinearLayout。

  final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;


            for(int i=1; i<=9;i++)
            {
                Button btnNums = new Button(this);
            final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;


            for(int i=1; i<=9;i++)
            {
                Button btnNums = new Button(this);
                btnNums.setText(i+"");
                ll.addView(btnNums);
                if(i%3==0){
                    ll2[i] = ll;
                    ll = null;

                }
            }

            layout.addView(ll2[0]);

    btnNums.setText(i+"");
            ll.addView(btnNums);
            if(i%3==0){
                ll2[i] = ll;
                ll = null;

            }
        }

        layout.addView(ll2[0]);

This does not work. 这是行不通的。 I get no error but when o run the app, it is stopped to work. 我没有收到任何错误,但是当o运行该应用程序时,它停止工作。 What's the problem? 有什么问题?

I used it in my Project and it worked for me, i used it like this. 我在项目中使用了它,并为我工作,就像这样。 Hope it helps 希望能帮助到你

Declare empty array at class level: 在类级别声明空数组:

LinearLayout[] imageLayoutContainers = new LinearLayout[10];

And then in on onCreate methord : 然后在onCreate methord上:

 for (int i = 0; i < imageLayoutContainers.length; i++) {
        imageLayoutContainers[i] = new LinearLayout(this);
        imageLayoutContainers[i].setOrientation(LinearLayout.VERTICAL);
        imageLayoutContainers[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        imageLayoutContainers[i].setBackgroundResource(imagesIds[i]);
    }

It worked, thanks 很好,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM