2017-04-10 37 views
-1

4個角落,我想4次增加4個角編程屏幕,添加4次以編程方式使用ALIGN_PARENT

但由於需要它不工作。請幫助我。

View[] TchBoxAryVar = new View[4]; 
int LyoRulAryVar[] = {RelativeLayout.ALIGN_PARENT_TOP, 
        RelativeLayout.ALIGN_PARENT_TOP | 
        RelativeLayout.ALIGN_PARENT_RIGHT, 
        RelativeLayout.ALIGN_PARENT_BOTTOM, 
        RelativeLayout.ALIGN_PARENT_BOTTOM | 
        RelativeLayout.ALIGN_PARENT_RIGHT}; 
int ClrAryVar[] = {Color.RED, Color.GREEN, Color.GRAY, Color.MAGENTA}; 

for(int IdxVal = 0; IdxVal < TchBoxAryVar.length; IdxVal++) 
{ 
    Log.d("TAG", "onCreate: " + IdxVal); 
    TchBoxAryVar[IdxVal] = new View(this); 

    RelativeLayout.LayoutParams LyoRulVar = new RelativeLayout.LayoutParams(200, 200); 
    LyoRulVar.addRule(LyoRulAryVar[IdxVal]); 
    TchBoxAryVar[IdxVal].setLayoutParams(LyoRulVar); 
    TchBoxAryVar[IdxVal].setBackgroundColor(ClrAryVar[IdxVal]); 
    MainViewVav.addView(TchBoxAryVar[IdxVal]); 
} 
+0

感謝您支持我的工作答案... – AlexTa

回答

3

Bidimensional array does not the trick。您不能使用「|」以編程方式設置多個校準分隔器。試試下面的例子:

final int LyoRulAryVar[][] = new int[][]{ 
      {RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_LEFT}, 
      {RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.ALIGN_PARENT_RIGHT}, 
      {RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.ALIGN_PARENT_LEFT}, 
      {RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.ALIGN_PARENT_RIGHT} 
    }; 
    int ClrAryVar[] = {Color.RED, Color.GREEN, Color.GRAY, Color.MAGENTA}; 

    for (int IdxVal = 0; IdxVal < TchBoxAryVar.length; IdxVal++) { 
     TchBoxAryVar[IdxVal] = new View(this); 
     RelativeLayout.LayoutParams LyoRulVar = new RelativeLayout.LayoutParams(200, 200); 
     for (int j = 0; j < LyoRulAryVar[IdxVal].length; j++) { 
      LyoRulVar.addRule(LyoRulAryVar[IdxVal][j]); 
     } 
     TchBoxAryVar[IdxVal].setLayoutParams(LyoRulVar); 
     TchBoxAryVar[IdxVal].setBackgroundColor(ClrAryVar[IdxVal]); 
     MainViewVav.addView(TchBoxAryVar[IdxVal]); 
    }