android gridlayout行和列与结果不匹配 [英] android gridlayout row and column not match the result

查看:38
本文介绍了android gridlayout行和列与结果不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个GridLayout

这是我在服务器上的设置:

 items.add(listOf(4, 3, 0, 0)) ->从 x: 0 - y:0 开始水平展开:4,垂直 3items.add(listOf(1, 1, 0, 3)) ->从 x: 0 - y:3 开始水平展开:1,垂直展开 1items.add(listOf(1, 1, 1, 3)) ->从 x 开始:1 - y:3 水平展开:1,垂直 1items.add(listOf(1, 1, 2, 3)) ->从 x 开始:2 - y:3 水平展开:1,垂直 1items.add(listOf(1, 1, 3, 3)) ->从 x 开始:3 - y:3 水平展开:1,垂直 1

这是我绘制表格的代码:

私人乐趣populateTable(){val items = AppUtil.getListItemsForGroupLayout(layoutCode)val totalRows = AppUtil.getRowOrColumns(layoutCode) ->4 行val totalCols = AppUtil.getRowOrColumns(layoutCode) ->4 列tableView.columnCount = totalColstableView.rowCount = totalRowsitems.forEach {val 参数 = GridLayout.LayoutParams()params.width = 0参数.高度 = 0params.rowSpec = GridLayout.spec(it[3], it[1].toFloat())params.columnSpec = GridLayout.spec(it[2], it[0].toFloat())val view = TextView(this@CallScreen)view.text = "${it[3]} - ${it[2]} - ${it[1]} - ${it[0]}";view.setBackgroundColor(Int.randomColor())view.layoutParams = 参数tableView.addView(视图)}}

但结果不一样:

有人可以帮我解决这个问题吗?谢谢

解决方案

 params.rowSpec = GridLayout.spec(it[3], it[1].toFloat())params.columnSpec = GridLayout.spec(it[2], it[0].toFloat())

这里您使用的是

I want to create a GridLayout

Here is my setting from server:

 items.add(listOf(4, 3, 0, 0)) -> start at x: 0 - y:0 expand horizontal : 4, vertical 3
 items.add(listOf(1, 1, 0, 3)) -> start at x: 0 - y:3 expand horizontal : 1, vertical 1
 items.add(listOf(1, 1, 1, 3)) -> start at x: 1 - y:3 expand horizontal : 1, vertical 1
 items.add(listOf(1, 1, 2, 3)) -> start at x: 2 - y:3 expand horizontal : 1, vertical 1
 items.add(listOf(1, 1, 3, 3)) -> start at x: 3 - y:3 expand horizontal : 1, vertical 1           

And here is my code to draw the table:

private fun populateTable(){
        val items = AppUtil.getListItemsForGroupLayout(layoutCode)
        val totalRows = AppUtil.getRowOrColumns(layoutCode) -> 4 rows
        val totalCols = AppUtil.getRowOrColumns(layoutCode) -> 4 columns

        tableView.columnCount = totalCols
        tableView.rowCount = totalRows

        items.forEach {
            val params = GridLayout.LayoutParams()
            params.width = 0
            params.height = 0

            params.rowSpec = GridLayout.spec(it[3], it[1].toFloat())
            params.columnSpec = GridLayout.spec(it[2], it[0].toFloat())

            val view = TextView(this@CallScreen)
            view.text = "${it[3]} - ${it[2]} - ${it[1]} - ${it[0]}"
            view.setBackgroundColor(Int.randomColor())
            view.layoutParams = params
            tableView.addView(view)
        }
    }

But the result is not the same:

Can someone tech me the problem? Thanks

解决方案

   params.rowSpec = GridLayout.spec(it[3], it[1].toFloat())
   params.columnSpec = GridLayout.spec(it[2], it[0].toFloat())

Here you are using this version that provides only the start & weight values.

Looking into other overloaded versions of GridLayout.spec, there is another parameter (size) that also controls this behavior as by looking into the GridLayout source code, all these overloaded methods call the private constructor:

    private Spec(boolean startDefined, int start, int size, Alignment alignment, float weight) {
        this(startDefined, new Interval(start, start + size), alignment, weight);
    }

Solution:

To fix this we need to use the size parameter by using this overloaded version in setting the params.columnSpec:

So, in your code replace:

params.columnSpec = GridLayout.spec(it[2], it[0].toFloat())

With:

params.columnSpec = GridLayout.spec(it[2], it[0], 1f)

Preview:

这篇关于android gridlayout行和列与结果不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆