Как сделать представления такого же размера в tablelayout, если каждая строка содержит различное количество просмотров?
Я создал 4 кнопки рисунка и добавил их в список кнопок изображения GIF:
for (int i = 0; i < 4; ++i) {
GifImageButton myButton = new GifImageButton();
myButton.setBackgroundResource(drawables[i]); // add some drawable to the button background
myButton.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1.0f));
listButtons.add(myButton);
}
Затем я создал TableLayout (строки = 2, столбцы = 3) программен и добавил его в мой макет:
MyTableLayout tableLayout = new MyTableLayout(this);
tableLayout.createTableLayoutOfButtons(tableRows /*=2*/, tableCols /*=3*/, listButtons);
mylinearLayout.addView(tableLayout);
мой MyTableLayout класс:
public class MyTableLayout extends TableLayout {
public MyTableLayout(Context context) {
super(context);
}
// indexListButtons is the current index of the listButtons elements. so as long as there are buttons, we will add them to the table rows
int indexListButtons = 0;
public void createTableLayoutOfButtons(int numRows, int numCols, List<GifImageButton> listButtons) {
setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
for (int i = 0; i < numRows; ++i) {
TableRow tableRow = new TableRow(getContext());
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT));
for (int j = 0; j < numCols; ++j, ++indexListButtons) {
// indices 0, 1, 2, 3
if (indexListButtons < listButtons.size()) {
tableRow.addView(listButtons.get(indexListButtons));
}
// indices 4, 5 don't exist
else {
// not enough buttons
}
}
addView(tableRow);
}
}
}
Как я могу изменить код, чтобы получить «ожидаемый результат»?
Я пробовал макет, но это не сработало. Я хотел попытаться избежать вставки пустых просмотров, но он решил это! Мне нужно было добавить представление с конкретными параметрами представления. благодаря! –
Ницца! Рад, что вы его работали. – Mike