所以我有这个界面,当我单击按钮时,我将新项目添加到TableLayout中。 See this picture
正如您在上图中所看到的,问题在于,与其在第三列的顶部添加新项,不如在第三列中将其添加为新行。因此,当listsize大于5时,新项目应在第三列的第二列中添加线性。我只能通过添加新行来解决。我不知道如何解决这个问题。
这是代码
fun addPlayerToTable(player: String) {
//Create new Row and TextView
val newRow = TableRow(this)
val newText = TextView(this)
var layoutParams = TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT)
//Set text on ViewText
newText.text = player
if(players.size > 5){
layoutParams.column = 2
}else {
layoutParams.column = 1
}
//Add TextViews and Rows dynamically
newRow.addView(newText, layoutParams)
table!!.addView(newRow)
}
最佳答案
所以我实际上解决了。该修复程序是根据某些条件添加了新的TextViews和TableRows。还要从索引1-6中删除 View ,因为我们不想删除0索引。不过,它可能可以更简单地完成。
fun addPlayerToTable(player: String) {
//Create new Row and TextView
var newRow = TableRow(this)
var newText = TextView(this)
var list = mutableListOf<TableRow>()
var texts = mutableListOf<TextView>()
if(players.size > 5){
for(i in 0 until players.size){
list.add(TableRow(this))
texts.add(TextView(this))
texts[i].text = players[i]
}
}
var layoutParams = TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT)
layoutParams.column = 1
//Set text on ViewText
newText.text = player
if(players.size > 5){
for(i in 1 until 6){
table!!.removeViewAt(1)
}
for(i in 0 until 5){
if(i + 5 >= players.size){
list[i].addView(texts[i], layoutParams)
table!!.addView(list[i])
}
else{
list[i].addView(texts[i], layoutParams)
list[i].addView(texts[i+5], layoutParams)
table!!.addView(list[i])
}
}
}else {
//Add TextViews and Rows dynamically
newRow.addView(newText, layoutParams)
table!!.addView(newRow)
}
}
关于android - 将数据添加到TableLayout的新列中的现有行-Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52952739/