سؤال

I need to create this layout purely in Java and not in XML. This means I have no XML in Eclipse for this project. Right now my layout looks like this:

enter image description here

You might not be able to tell from the picture but blocks 0, 1 and 3 are all on top of each other. I need the 5 blocks to look like this:

 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. 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?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top