就像标题所说,我在将TableViews均匀地传播到tableRows时遇到了问题。我的xml是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar/>
<TextView/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/progressBar">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</ScrollView>
在Activity启动后,将使用以下代码创建动态表:
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.setId(i + 1);
tableRow.setBackgroundResource(R.mipmap.shelfboard);
tableRow.setPadding(20, 0, 20, 0);
tableRow.setOnClickListener(thisvariable);
tableLayout.addView(tableRow);
}
现在我想为每个tableRow为每个tableRow添加不同数量的相同宽度的ImageView。
我有4个TableRows
我的代码是:
for (int i = 0; i < adpList.size(); i++) {
int row = adpList.get(i).getShelfBoard();
if (row==1) {
int productsPerBoard = 2;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==2) {
int productsPerBoard = 5;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
productPicture.setBackgroundResource(R.drawable.border);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==3) {
int productsPerBoard = 3;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==4) {
int productsPerBoard = 7;
int width = tableLayout.getWidth() / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
productPicture.setBackgroundResource(R.mipmap.ic_launcher);
r.addView(productPicture);
不幸的是,每行只有两张图片。它总是占用第一块板的宽度。我的输出:
如何为每行添加不同数量的ImageView?
答案 0 :(得分:0)
r = (TableRow) findViewById(shelfboard);
productPicture = new ImageView(this);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT, 1f);
tlp.setMargins(0, 20, 0, 50);
productPicture.setLayoutParams(tlp);