我在 LinearLayout 中定义了一个表格布局,如下所示:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#E6E6E6"
android:orientation="vertical" >
<TableLayout
android:id="@+id/fbTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@android:color/darker_gray"
android:stretchColumns="*" >
</TableLayout>
</LinearLayout>
我将动态行添加到 TableLayout,如下所示:
fbTableLayout = (TableLayout) findViewById(R.id.fbTableLayout);
for (int i = 0; i < 4; i++) {
fbTableRow = new TableRow(this);
fbTableRow.setBackgroundColor(Color.WHITE);
LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
fbTableRow.setLayoutParams(layoutParams);
ImageView iv = new ImageView(this);
iv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
iv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
0.25f));
TextView tv = new TextView(this);
tv.setText("Album "+ i);
tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT,
0.75f));
fbTableRow.addView(iv);
fbTableRow.addView(tv);
fbTableLayout.addView(fbTableRow, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
但是我无法在生成的行之间生成空格。布局如附图所示。
我已经尝试了 stackoverflow 中给出的许多解决方案来解决此问题,但没有一个对我有用。不知道我错过了什么。 任何帮助将不胜感激。 提前致谢。
最佳答案
添加另一个带有空白图像的表行(在循环中)。指定图像的尺寸以创建您的空间所需的尺寸。
关于java - Android - 在动态创建的表格行之间留出空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14128930/