0

我正在尝试创建一个不GridView使用仅 a的网格RelativeLayout。问题是根据我的日志,应该正确设置自定义视图(圆圈),但我只看到第一行和第一行。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mainLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    mainLayout.setLayoutParams(mParams);

    itemsLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams iParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    itemsLayout.setLayoutParams(iParams);       

    grid = new TTTGrid(this, "#ffdd00", _gridCount, _gridSize, false);

    TTTItem recent = null;
    RelativeLayout.LayoutParams rl;
    int k = 0;

    for(int i = 0; i < (_gridCount*_gridCount); i++) {
        circles[k] = new TTTItem(this, _gridSize, "black");
        circles[k].setId((int)k+100);
        circles[k].setOnClickListener(itemClickHandler);
        circleIDs[k] = circles[k].getId();

        rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if(recent == null) {
            Log.d("DEBUGGY", "" + "recent == null on k: " + k + "/" + circles[k].getId());
            rl.addRule(RelativeLayout.BELOW);
        }
        else {                          
            //if(modulo(k,_gridCount) == 0 && k > 0) {
            if(k >= _gridCount) {
                Log.d("DEBUGGY", "" + "circle: " + k + "/" + circles[k].getId() + " set below: " + (k-_gridCount) + "/" + circles[k-_gridCount].getId());
                rl.addRule(RelativeLayout.BELOW, circles[k-_gridCount].getId());
            }
            else {
                Log.d("DEBUGGY", "" + "circle: " + k + "/" + circles[k].getId() + " set RIGHT OF: " + (k-1) + "/" + circles[k-1].getId());
                rl.addRule(RelativeLayout.RIGHT_OF, circles[k-1].getId());
            }
            //else {
            //  rl.addRule(RelativeLayout., recent.getId());
            //}                             
        }               

        recent = circles[k];
        itemsLayout.addView(circles[k], rl);            
        k++;
    }


    Toast.makeText(this, "circles: " + circles.length, Toast.LENGTH_SHORT).show();


    mainLayout.addView(grid);
    mainLayout.addView(itemsLayout);

    setContentView(mainLayout);
}

好的,我将其更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mainLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mainLayout.setLayoutParams(mParams);

    itemsLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams iParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    //iParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    itemsLayout.setLayoutParams(iParams);       

    grid = new TTTGrid(this, "#ffdd00", _gridCount, _gridSize, false);

    RelativeLayout.LayoutParams rl;
    for(int i=0; i < _gridCount; i++) {
        for(int j = 0; j < _gridCount; j++) {
            circles[i][j] = new TTTItem(this, _gridSize, "black");
            circles[i][j].setId((int)(j*i)+100);
            circles[i][j].setOnClickListener(itemClickHandler);
            circleIDs[i][j] = circles[i][j].getId();

            rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            rl.leftMargin = _gridSize*i;
            rl.topMargin = _gridSize*j;             

            itemsLayout.addView(circles[i][j], rl);         

        }
    }

    Toast.makeText(this, "circles: " + circles.length, Toast.LENGTH_SHORT).show();

    mainLayout.addView(grid);
    mainLayout.addView(itemsLayout);

    setContentView(mainLayout);
}
4

1 回答 1

0

您不应该使用边距来定位项目,请使用以下规则RelativeLayout

    RelativeLayout.LayoutParams rl;
    int belowId = 100; // starting id
    for (int i = 0; i < (_gridCount * _gridCount); i++) {
        circles[i] = new TTTItem(this, _gridSize, "black");     
        circles[i].setId(i + 100);
        circles[i].setOnClickListener(itemClickHandler);
        circleIDs[i] = circles[i].getId();
        rl = new RelativeLayout.LayoutParams(40, 40);
        // update the below id if we need to
        if (i != 0 && i % _gridCount == 0) {
            belowId = circleIDs[i - 1];
        }                        
        if (i % _gridCount == 0) {
            rl.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        } else {
            rl.addRule(RelativeLayout.RIGHT_OF, circleIDs[i - 1]);
        }           
        if (i < _gridCount) {
            rl.addRule(RelativeLayout.ALIGN_PARENT_TOP);                
        } else {                
            rl.addRule(RelativeLayout.BELOW, belowId);
        }
        itemsLayout.addView(circles[i], rl);
    }
于 2013-02-21T10:57:41.583 回答