简体   繁体   English

创建不带XML文件的RelativeLayout

[英]Creating RelativeLayout with no XML file

I need to create this layout purely in Java and not in XML. 我需要仅使用Java而不是XML来创建此布局。 This means I have no XML in Eclipse for this project. 这意味着我在Eclipse中没有针对该项目的XML。 Right now my layout looks like this: 现在,我的布局如下所示:

在此处输入图片说明

You might not be able to tell from the picture but blocks 0, 1 and 3 are all on top of each other. 您可能无法从图片中分辨出来,但是块0、1和3彼此重叠。 I need the 5 blocks to look like this: 我需要5个块才能看起来像这样:

 0  1  2
 3  4

This is my first time not using an XML file to make Activity layouts so I am inching along with figuring this stuff out. 这是我第一次不使用XML文件制作“活动”布局,因此我正在逐步弄清楚这些内容。 And if you notice me doing something entirely the "long" way when I could write the code more efficiently, let me know please. 而且,如果您发现我在更高效地编写代码时完全以“漫长的”方式进行工作,请告诉我。

But my question is, why aren't the blocks laying the order I need them? 但是我的问题是,为什么这些障碍物不能按我需要的顺序排列? I feel like I am messing something up that is simple. 我觉得我在搞弄简单的事情。

public class Puzzle extends Activity {

    int z1to5 = 50;
    int z6to10 = 50;

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

        RelativeLayout relativelayout = new RelativeLayout(getApplicationContext());

        Button bList[] = new Button[5];

        for (int i = 0; i < 5; i++) {
            Button button = new Button(getApplicationContext());
            button = new Button(getApplicationContext());
            button.setId(i);
            button.setText("" + i);
            relativelayout.addView(button);
            bList[i] = button;
        }

        RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(150, 150);
            bList[0].setLayoutParams(params0);

        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(150, 150);
            params1.addRule(RelativeLayout.RIGHT_OF, bList[0].getId());
            bList[1].setLayoutParams(params1);

        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(150, 150);
            params2.addRule(RelativeLayout.RIGHT_OF, bList[1].getId());
            bList[2].setLayoutParams(params2);

        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(150, 150);
            params3.addRule(RelativeLayout.BELOW, bList[0].getId());
            bList[3].setLayoutParams(params3);

        RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(150, 150);
            params4.addRule(RelativeLayout.RIGHT_OF, bList[3].getId());
            params4.addRule(RelativeLayout.BELOW, bList[1].getId());
            bList[4].setLayoutParams(params4);

        setContentView(relativelayout);
    }
}

I got it working with this change: 我可以使用此更改:

for (int i = 0; i < 5; i++) {
        Button button = new Button(getApplicationContext());
        button = new Button(getApplicationContext());
        button.setId(i+1);
        button.setText("" + i);
        relativelayout.addView(button);
        bList[i] = button;
    }

button.setId(i+1);

I can't find it in the docs, but maybe when you make a new View programmatically, the default NO_ID equals 0 ? 我在文档中找不到它,但是也许当您以编程方式创建一个新View时,默认NO_ID等于0吗?

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

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