代码之家  ›  专栏  ›  技术社区  ›  jiwopene

android如何在GridLayout中留下空白

  •  0
  • jiwopene  · 技术社区  · 7 年前

    我有 GridLayout (来自支持库)其中有两行按钮。所有按钮的宽度设置为2,高度设置为1。第一行应向右移动一个位置。当我这么做的时候 网格布局

    第二排:

    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
    params.rowSpec = GridLayout.spec(1, 1, 1);
    params.columnSpec = GridLayout.spec(x, 2, 1);
    

    第一行:

    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
    params.rowSpec = GridLayout.spec(0, 1, 1);
    params.columnSpec = GridLayout.spec(x + 1, 2, 1);
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   jiwopene    7 年前

    必须将虚拟视图插入到顶部和左侧的所有单元格(开始)。

    for (int x = 0; x < gridLayout.getColumnCount(); x++) {
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.rowSpec = GridLayout.spec(0, 1, 1);
        params.columnSpec = GridLayout.spec(x, 1, 1);
        params.setGravity(Gravity.FILL);
        params.width = 0;
        gridLayout.addView(new Space(this), params);
    }
    for (int y = 0; y < gridLayout.getRowCount(); y++) {
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.rowSpec = GridLayout.spec(y, 1, 1);
        params.columnSpec = GridLayout.spec(0, 1, 1);
        params.setGravity(Gravity.FILL);
        params.width = 0;
        gridLayout.addView(new Space(this), params);
    }
    
        2
  •  0
  •   J.C    6 年前

    这是我提出的一个更好的(IMO)解决方案;

    <GridLayout
        android:orientation="horizontal"
        android:columnCount="3"
        android:useDefaultMargins="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <Space android:layout_columnWeight="1" />
        <Space android:layout_columnWeight="1" />
        <Space android:layout_columnWeight="1" />
    
        <TextView
            android:text="01"
            android:layout_columnWeight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            android:background="@color/colorBlack"
            android:padding="15dp" />
    
        <TextView
            android:text="02"
            android:layout_columnWeight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            android:background="@color/colorBlack"
            android:padding="15dp" />
    
    
        <!-- REPEAT ITEM HOWEVER MANY TIMES -->
    
    
    </GridLayout>
    

    下面是它如何呈现网格中的4、3、2和1项,并将默认边距添加到GridLayout中;

    Shows how GridLayout renders with 4, 3, 2 or 1 items

    推荐文章