【问题标题】:Set rowSpan or colSpan of a child of a GridLayout programmatically?以编程方式设置 GridLayout 子级的 rowSpan 或 colSpan?
【发布时间】:2012-11-11 23:53:56
【问题描述】:

我有一个GridLayout,有 5 列和 3 行。现在我可以插入任意子视图,这很棒。更好的是,我可以将 columnSpan=2 分配给某个项目,以便将其跨越到 2 列(与 rowSpan 相同)。

现在的问题是,我无法以编程方式(即在运行时)分配 rowSpan 或 columnSpan。一些搜索建议是这样的:

layoutParams.columnSpec = GridLayout.spec(0, columnSpan);

但我不太明白规范的参数是什么意思(开始和大小)。在这一点上,文档也很差。

非常感谢任何帮助!

【问题讨论】:

    标签: android html grid-layout html-table columnspan


    【解决方案1】:

    好的,我花了几个小时弄清楚这里发生了什么。好吧,我没有找到在运行时设置 columnSpan 或 rowSpan 的任何工作方式。

    但我找到了一个可行的解决方案(至少对我来说):

    Java 代码

    private LinearLayout addNewSpannedView(Integer resourceId, ViewGroup rootElement) {
        return (LinearLayout) ((ViewGroup) getLayoutInflater().inflate(resourceId, rootElement, true)).getChildAt(rootElement.getChildCount() - 1);
    }
    // set columnSpan depending on some logic (gridLayout is the layout to add the view's to -> in my case these are LinearLayouts)
    shape = addNewSpannedView(columnSpan == 1 ? R.layout.grid_ll_col_span_1 : R.layout.grid_ll_col_span_2, gridLayout);
    

    grid_ll_col_span_2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="@dimen/shapeWidth"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"/>
    

    提示:设置宽度和高度属性非常重要, inflate() 将视图添加到根元素(即父元素)。

    我希望有人可以使用它;-)

    【讨论】:

    • 那么 shape 是 LinearLayout 吗?如果我想将 ImageButton 添加到我的 GridLayout 怎么办?
    • 你好..需要同样的东西...你能建议更多
    【解决方案2】:
        GridLayout gridLayout = (GridLayout)findViewById(R.id.tableGrid);
    
        gridLayout.removeAllViews();
    
        int total = 12;
        int column = 5;
        int row = total / column;
        gridLayout.setColumnCount(column);
        gridLayout.setRowCount(row + 1);
        for(int i =0, c = 0, r = 0; i < total; i++, c++)
        {
            if(c == column)
            {
                c = 0;
                r++;
            }
            ImageView oImageView = new ImageView(this);
            oImageView.setImageResource(R.drawable.ic_launcher);
            GridLayout.LayoutParams param =new GridLayout.LayoutParams();
            param.height = LayoutParams.WRAP_CONTENT;
            param.width = LayoutParams.WRAP_CONTENT;
            param.rightMargin = 5;
            param.topMargin = 5;
            param.setGravity(Gravity.CENTER);
            param.columnSpec = GridLayout.spec(c);
            param.rowSpec = GridLayout.spec(r);
            oImageView.setLayoutParams (param);
            gridLayout.addView(oImageView);
        }
    

    【讨论】:

    • 问题是关于以编程方式设置跨度。但最重要的答案是简单地将视图添加到网格布局......太有趣了
    【解决方案3】:

    这个怎么样?

    GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
    layoutParams.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, item.getRowSpan());
    layoutParams.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, item.getColumnSpan());
    

    根据文档,如果在将子视图插入 GridLayout 之后更改列或行跨度,您还应该在 GridLayout 上调用 setLayoutParams。如果在设置跨度时添加它们,则不需要它

    【讨论】:

    • 这是正确的答案。 GridLayout.spec(start, size) 的参数实际上分别设置了行(或列)和跨度(所以也许应该是 GridLayout.spec(rowOrColumn, span))。非常糟糕的文档。
    【解决方案4】:

    我正在以编程方式更改大约 2 个单元格的列跨度,并且 通过以下代码膨胀:

    GridLayout.LayoutParams itemLP = (GridLayout.LayoutParams)gridItemV.getLayoutParams();
    itemLP.columnSpec = GridLayout.spec(0, 2);
    gridItemV.setLayoutParams(itemLP);
    

    重要的是在更改columnSpec 后致电setLayoutParams 希望对你也有帮助

    【讨论】:

      【解决方案5】:

      您可以像这样修改 GridLayout layout_rowSpan 属性:

      // View allocated in first row and first column
      View child = findViewById(R.id.row0column0);
      
      GridLayout.LayoutParams params =
         new GridLayout.LayoutParams(child.getLayoutParams());
      
      params.rowSpec = GridLayout.spec(0, 2);    // First cell in first row use rowSpan 2.
      params.columnSpec = GridLayout.spec(0, 2); // First cell in first column use columnSpan 2.
      child.setLayoutParams(params);
      

      我同意文档在这方面需要一些改进。

      【讨论】:

        猜你喜欢
        • 2016-12-09
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        • 2014-10-09
        • 2013-01-16
        • 1970-01-01
        • 2013-05-14
        相关资源
        最近更新 更多