【发布时间】:2019-09-18 13:51:05
【问题描述】:
我正在创建一个由 10X10 单元格组成的表格,但它(在创建所有单元格之后)不适合其父级。
- 为了让我在展示我的代码之前更容易理解,表格如下所示:
如您所见,每个表格单元格都是一个带有“测试”作为文本的按钮,我无法让这个 10X10 网格展开以填充其父级(视图水平退出屏幕并且不会垂直填充)
我正在像这样以编程方式创建每个单元格:
val table = findViewById<TableLayout>(R.id.table)
for (i in 0..9) { //10 rows inside the table
val row = TableRow(this)
for (j in 0..9) { //10 cells inside every row
val cell = Button(this)
cell.layoutParams = TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT
)
cell.text = "test"
cell.setTextColor(Color.RED)
cell.setBackgroundColor(Color.WHITE)
cell.setPadding(0, 0, 0, 0)
row.addView(cell)
}
table.addView(row)
}
-
我的下一步是更改为每个单元格设置
layoutParams的方式,我尝试更改每个单元格的尺寸,如下所示:cell.layoutParams = TableRow.LayoutParams( //table = the view that contains all cells table.width/5, //make the cell width = 20% of the parent width table.height/10 //make the cell height= 10% of the parent height )
由于某种原因,这使整个表格消失了,将 layoutparams 设置为 MATCH_PARENT 与 WRAP_CONTENT 具有相同的效果。
我检查了android - setting LayoutParams programmatically 和LayoutParams,但没有找到答案。
我做错了什么使我的表格消失了,我怎样才能使单元格适合表格大小?
【问题讨论】:
标签: android android-layout kotlin