2013-02-20 32 views
0

我试图创建一个没有GridView的网格,只使用RelativeLayout。问题是根据我的日志自定义视图(圆圈)应设置正确,但我只看到第一行和第一行。使用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); 
} 
+0

*,但我只看到第一行和第一行* - 这意味着什么(我指的是第一行部分)? – Luksprog 2013-02-20 17:57:42

+0

根据我的代码“圈子”应该设置在一个网格..但是当我在我的设备上测试它只显示第一行..和第一列.. – Jakepot 2013-02-21 09:13:52

回答

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); 
    } 
+0

谢谢!它只显示2行... – Jakepot 2013-02-26 15:45:04

+0

@Jakepot我不知道在测试代码时会如何发生。对于'_gridCount'值你运行我的代码? – Luksprog 2013-02-28 09:25:23