【发布时间】:2015-03-21 14:33:08
【问题描述】:
我是 Android 编程的新手。我想要一个具有 3 行和 3 列的表格布局(可能我会动态更改这些数字),每个表格行内部都有一个相对布局。这类似于 Google Play 商店中显示的视图,它显示了 Play 等的新来者。我尝试使用按钮来实现这一点。但我想膨胀一个 xml 文件,以相对布局为根,使用 ImageView 和 TextView。这可能吗?或者实现这种布局的最佳方式是什么?此外,如何找到膨胀布局的图像视图和文本视图,以便我可以以编程方式为其添加属性。这是我尝试过的。
private void populateCategoryGrid() {
TableLayout categoriesLayout;
TableRow row;
Button btn_category; int category_count = 12;
int NUM_ROW = category_count/3;
int NUM_COL = category_count/4;
for (int r=0;r<NUM_ROW;r++) {
row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f
));
categoriesLayout.addView(row);
for (int c = 0; c <NUM_COL; c++) {
btn_category = new Button(this);
btn_category.setId(c);
btn_category.setPadding(0, 0, 0, 0);
btn_category.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
1.0f
));
row.addView(btn_category);
}
}
}
【问题讨论】:
标签: java android android-layout layout dynamic-programming