5つのボタンの後に新しい行を作成する必要があるこのテーブルレイアウトではいくつかの問題があります。また、私はbutton.setText(lesson.getId());を追加するとクラッシュします。1行に5つのボタンを動的にロード
LesSelectionActivity.java
public static final int LESSON_ROW_COUNT = 5;
public void setButtonLessons() {
//draw LesSelection
setContentView(R.layout.activity_drumles);
TableLayout layout = (TableLayout) findViewById(R.id.les_select_layout);
int buttonIdCounter = 0;
for (Lesson lesson : getArrayLesson()) {
int columnCounter = 0;
TableRow tr = new TableRow(this);
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(30, 0, 30, 0);
tr.setLayoutParams(params);
layout.addView(tr);
if (columnCounter % LESSON_ROW_COUNT == 0) {
tr = new TableRow(this);
params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(30, 0, 30, 0);
tr.setLayoutParams(params);
layout.addView(tr);
}
Button button = new Button(this);
button.setId(buttonIdCounter);
//button.setText(lesson.getId());
button.setOnClickListener(this);
button.setBackgroundResource(R.drawable.buttonsoranje);
TableRow.LayoutParams paramsRow = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
paramsRow.column = columnCounter % LESSON_ROW_COUNT;
params.gravity = Gravity.CENTER_HORIZONTAL;
tr.addView(button);
buttonIdCounter++;
columnCounter = (columnCounter + 1) % LESSON_ROW_COUNT;
}
}
それでは、なぜのsetTextは動作しませんが、setBackgroundResourceはないでしょうか? (私が「//」を持っていることに注意してください。なぜなら、awnserは「remove //」ではないからです)
なぜ1行に1つのボタンしかありませんか?
"for(レッスンレッスン:getArrayLesson()){"が高く設定され、ifのすぐ上に配置されていれば機能しました。 – Jaxhead