我想給每個button創建一個新的LinearLayout。最後實現排序數字。
1 2 3
4 5 6
7 8 9
需要創建一個新的LinearLayout
作為HORIZONTAL
。但是怎麼在循環中創建?
for (int i=1:i<=9:i++) {
Button b = new Button(this);
b.setText(""+i);
// I need to do something here and put my general layout
}
可以像下面一樣試試:
LinearLayout outer = new LinearLayout(this);
outer.setOrientation(LinearLayout.VERTICAL);
LinearLayout inner;
for(int i = 0; i < 9; i++) {
if(i % 3 == 0) {
inner = new LinearLayout(this);
outer.addView(inner);
}
// Create your Buttons and add them to inner
}
setContentView(outer);