View fills all the available space

狂风中的少年 提交于 2019-12-11 09:04:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!