动态GridLayout

时间:2013-02-06 11:39:52

标签: android

我在另一个班级犯了错误,这就是为什么它不起作用。下面的代码似乎是正确的

我正在尝试创建动态GridLayout。在另一个类中,不是这个,我有一个方法来设计我的gridlayout的行和列。在下面的课程中,我为GridLayout添加了一些按钮:

int buttons= 6;//the number of bottons i have to put in GridLayout
int buttonsForEveryRow = 3; // buttons i can put inside every single row
int buttonsForEveryRowAlreadyAddedInTheRow =0; // count the buttons added in a single rows
int columnIndex=0; //cols index to which i add the button
int rowIndex=0; //row index to which i add the button

for(int i=0; i < buttons;i++){          
    /*if numeroBottoniPerRigaInseriti equals numeroBottoniPerRiga i have to put the other buttons in a new row*/
    if(buttonsForEveryRowAlreadyAddedInTheRow ==buttonsForEveryRow ){
        rowIndex++; //here i increase the row index
        buttonsForEveryRowAlreadyAddedInTheRow  =0;  
        columnIndex=0; 
    }   

    Spec row = GridLayout.spec(rowIndex, 1); 
    Spec colspan = GridLayout.spec(columnIndex, 1);
    GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row, colspan);
    gridLayout.addView(button_to_add,gridLayoutParam);

    buttonsForEveryRowAlreadyAddedInTheRow ++;
    columnIndex++;

在下图中,您可以看到我得到的内容:缺少按钮3和6。我担心我没有正确使用GridLayout.spec

enter image description here

2 个答案:

答案 0 :(得分:13)

使用下面的代码,您可以使用列跨度和行跨度动态地将图像视图添加到网格布局。

    gridLayout = (GridLayout) findViewById(R.id.gridview);

    gridLayout.removeAllViews();

    int total = 10;
    int column = 3;
    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);            

        oImageView.setLayoutParams(new LayoutParams(100, 100));

        Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
        Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
        if (r == 0 && c == 0) {
            Log.e("", "spec");
            colspan = GridLayout.spec(GridLayout.UNDEFINED, 2);
            rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2);
        }
        GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                rowSpan, colspan);
        gridLayout.addView(oImageView, gridParam);


    }

答案 1 :(得分:2)

使用GridLayout时存在一些限制,以下引用取自documentation

  

“GridLayout不支持权重原则,如   按重量定义。通常,因此不可能   配置GridLayout以分配多余的空间   多行或多列之间的比例...用于完全控制   行或列中过多的空间分布;使用LinearLayout   子视图用于保存关联单元格组中的组件。“

这是一个使用LinearLayout子视图的小例子。 (我使用空间视图占用未使用的区域并将按钮推到所需的位置。)

    

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:orientation="horizontal">
    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />
    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:text="Button 2" />
    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>
    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" />
    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:text="Button 4" />
    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>