Table 带有粘性 header 和合适的文本

Table with sticky header and fitting text

我试图找到如何用简单的粘性 header 制作 table,我发现的只是 this blog,这很好,但远远超出了我的需要 -我只需要一个粘性 header 行和可滚动的内容,这应该是微不足道的....

我的第二个 table-related 问题是我需要显示具有不同字段的行

| XYZ | Lorem ipsum long long long line foo bar blah ipsum dolores est. | 7 | - |
| PQR | Ceterum autem censeo Carthaginem esse delendam.                 | 3 | ✓ |

其中长文本应根据需要截断,而短字段必须完整显示。我可以找到最宽的文本或使用 "WWW",然后添加填充(也许还有其他东西?)以获得适当的宽度约束......或者我应该子类化 TableLayout?

使用两个表并用百分比定义列宽,使它们匹配。这将解决您的两个要求:

Container twoTables = BorderLayout.center(mainTable);
twoTables.add(NORTH, headerTable);

定义宽度:

protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
    TableLayout.Constraint con = super.createCellConstraint(value, row, column);
    return con.wp(COLUMN_WIDTHS[column]);
}