0

我按照https://stackoverflow.com/a/7123217/2884981上的示例进行操作,但不幸的是,该行没有延伸到表格的整个宽度(请参阅黑色“边框”/背景如何在右侧如此之大而其他地方只有 1px 宽)。

我希望第二列扩展,因此它占用了右侧的所有可用空间。

边框太大

这是我正在使用的代码。请,如果有人知道我如何将正确的“边框”设置为 1px 而不是看起来那么糟糕,请告诉我。

        for (int i = 0; i < tasks.size(); i++)
    {
        TaskDetail taskDetail = tasks.get(i);

        TableRow row = new TableRow(this);

        // Set the background/border colour to black
        row.setBackgroundColor(Color.BLACK);
        row.setPadding(1, 1, 1, 1);

        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        layoutParams.setMargins(0,0,1,0);

        LinearLayout detailCell = new LinearLayout(this);
        detailCell.setBackgroundColor(Color.parseColor("#FFE6F0"));
        detailCell.setLayoutParams(layoutParams);

        LinearLayout valueCell = new LinearLayout(this);
        valueCell.setBackgroundColor(Color.WHITE);
        valueCell.setLayoutParams(layoutParams);    

        TextView detailName = new TextView(this);
        detailName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        detailName.setText(taskDetail.getName());
        detailName.setPadding(5, 10, 5, 5);
        detailName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TextView valueName = new TextView(this);
        valueName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        valueName.setText(taskDetail.getValue());
        valueName.setPadding(5, 10, 5, 5);
        valueName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        detailCell.addView(detailName);
        valueCell.addView(valueName);

        // Add to the screen layout
        row.addView(detailCell);
        row.addView(valueCell);

        table.addView(row);
    }
4

1 回答 1

1

weight1.0f行中的每一列设置如下:

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);

只需进行如下更改:

...
...

row.setPadding(1, 1, 1, 1);

TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
layoutParams.setMargins(0,0,1,0);

LinearLayout detailCell = new LinearLayout(this);

...
...
于 2013-11-06T04:15:54.040 回答