我正在创建一个应用程序,我需要显示一个包含900到1200个座位的座位布局。通过循环以编程方式创建具有自定义TextView的LinearLayouts,我设法生成了900个座位布局。但我很确定这绝对不是这样做的方法。
这就是我在做的事情:
// Seat Layout
LinearLayout linearLayoutSeatContainer;
static final LinearLayout[] rows = new LinearLayout[30];
static final TextView[][] seats = new TextView[30][30];
static final View[][] spaceVertical = new View[30][4];
static final View[] spaceHorizontal = new View[4];
// Populating the screen with seats
for (int i = 0; i < 30; i++) {
rows[i] = new LinearLayout(this);
rows[i].setOrientation(LinearLayout.HORIZONTAL);
// Each loop creates a row of seats
for (int j = 0; j < 30; j++) {
// To add horizontal space after every 10 seats
if (j % 10 == 0) {
spaceVertical[i][(j / 10)] = new View(this);
spaceVertical[i][(j / 10)].setLayoutParams(spaceVerticalParams);
rows[i].addView(spaceVertical[i][(j / 10)]);
}
seats[i][j] = new TextView(this);
seats[i][j].setId(i * 100 + j);
seats[i][j].setLayoutParams(textViewParams);
seats[i][j].setBackgroundResource(R.drawable.seat_circle_empty);
seats[i][j].setClickable(true);
seats[i][j].setOnClickListener(this);
// TODO: Delete randomized code for disabled seats
Random rand = new Random();
int num = rand.nextInt(20);
if (num % 5 == 0 && (i != 0 && j != 0))
seats[i][j].setEnabled(false);
rows[i].addView(seats[i][j]);
}
// Adding the last vertical space between screen and the last seat
spaceVertical[i][3] = new View(this);
spaceVertical[i][3].setLayoutParams(spaceVerticalParams);
rows[i].addView(spaceVertical[i][3]);
// To add horizontal space after every 10 rows of seats
if (i % 10 == 0) {
spaceHorizontal[i / 10] = new View(this);
spaceHorizontal[i / 10].setLayoutParams(spaceHorizontalParams);
linearLayoutSeatContainer.addView(spaceHorizontal[i / 10]);
}
linearLayoutSeatContainer.addView(rows[i]);
}
// Adding the last horizontal space between screen and the last row
spaceHorizontal[3] = new View(this);
spaceHorizontal[3].setLayoutParams(spaceHorizontalParams);
linearLayoutSeatContainer.addView(spaceHorizontal[3]);
请忽略该位随机化,这是出于测试目的。我知道这不是做事的方式。请指出我正确的方向。