Don't understand how to use GridLayout.spec()

后端 未结 1 1410
忘掉有多难
忘掉有多难 2020-12-16 01:12

This GridLayout is going in my app that has a lot of levels. Each level has a different number of rows and columns. I assume that this GridLayout would be my best bet to u

相关标签:
1条回答
  • 2020-12-16 01:45

    I didn't exactly understand your question, but here are some examples that explain the syntax:

    Spec row1 = GridLayout.spec(0, 2); //here you set row to be first row and it takes 2 cells in height.
    
    Spec row2  = GridLayout.spec(2); //this row goes under row1 and it takes 1 cell(default size = 1) 
    

    and etc.

    Spec col0 = GridLayout.spec(0); //same here - first column, width = 1 cell.
    
    Spec colspan2 = GridLayout.spec(0, 2);
    

    so you can do like this:

    Spec row1 = GridLayout.spec(0);
    Spec row2 = GridLayout.spec(1);
    Spec row3 = GridLayout.spec(2);
    Spec row4 = GridLayout.spec(3);
    
    Spec col0 = GridLayout.spec(0);
    Spec col1 = GridLayout.spec(1); 
    Spec col2 = GridLayout.spec(2);
    
    GridLayout gridLayout = new GridLayout(this);
    GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, col0);
    /*Here you can set options for first cell which is in first row and first column.*/
    first.width = screenWidth;
    first.height = quarterScreenWidth * 2;
    twoByTwo1.setLayoutParams(first);
    twoByTwo1.setGravity(Gravity.CENTER);
    twoByTwo1.setBackgroundColor(Color.RED);
    twoByTwo1.setText("TOP");
    twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large);
    gridLayout.addView(twoByTwo1, first)
    //You can set all cells like above.
    

    I hope this helps. :)

    0 讨论(0)
提交回复
热议问题