我想给每个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);