android - 将数据添加到TableLayout的新列中的现有行-Kotlin

标签 android kotlin tablelayout

所以我有这个界面,当我单击按钮时,我将新项目添加到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/

相关文章:

android - 在运行时删除工具栏项目

android - 通过处理程序的无限循环会阻塞 UI 线程吗?

swift - 为 Decodable 类中的属性设置更改名称 | swift

android - 现代Android中的后台处理

android - 如何将数据库打印为表格? (安卓,SQLite)

css - 表格单元格中的可滚动 div

android - 使用从上到下的滑动手势控制android中的音量

android - 在高层次上,gradle sync 在 Android 中如何工作?

android - 使用 Kotlin 组合整数标志的最佳方法?

html - 布局表?为什么不?