问题
I am trying to use GridLayout
programmatically.
Here is my code:
GridLayout p = new GridLayout(this);
p.setRowCount(20);
p.setColumnCount(20);
View c = new View(this);
c.setBackgroundColor(this.getResources().getColor(R.color.darkbluetheme));
Spec specusata = android.support.v7.widget.GridLayout.spec(1,2);
Spec specusata2 = android.support.v7.widget.GridLayout.spec(1,2);
p.addView(c,new LayoutParams(specusata,specusata2));
setContentView(p);
Here is the result I get:
While i was expecting something like this:
Please what am I doing wrong???
Thanks !!!
EDIT EDIT EDIT
This happens even if i set the LayoutParams of the View to WRAP_CONTENT:
GridLayout p = new GridLayout(this);
p.setRowCount(20);
p.setColumnCount(20);
View c = new View(DayActivity.this);
c.setBackgroundColor(this.getResources().getColor(R.color.darkbluetheme));
Spec specusata = android.support.v7.widget.GridLayout.spec(1);
Spec specusata2 = android.support.v7.widget.GridLayout.spec(1);
c.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
p.addView(c,new android.support.v7.widget.GridLayout.LayoutParams(specusata,specusata2));
setContentView(p);
回答1:
IMHO: Your other rows and columns are empty, and thus zero width. The rest of your space is filled with that single cell, and takes all space.
Try adding more child views with other colors, and then you will understand what happens.
来源:https://stackoverflow.com/questions/18568525/view-fills-all-the-available-space