2013-08-30 18 views
-1

这是我的布局文件:围绕ImageViews的矩阵屏幕的中心动态

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/imageLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:background="@color/silver"> 
     <TextView 
    android:id="@+id/bottom" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignBaseline="@id/imageLayout" 
    android:layout_alignBottom="@id/imageLayout" 
    android:background="@color/red" 
    android:gravity="center" 
    android:textColor="@color/white" 
    android:textSize="30sp" 
    /> 
<TextView 
    android:id="@+id/above" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:paddingBottom="3dp" 
    android:layout_above="@id/bottom" 
    android:background="@color/blue" 
    android:gravity="center" 
    android:textColor="@color/white" 
    android:textSize="30sp" 
</RelativeLayout> 

我加入ImageViews动态名为MatchGame.xml的有关布局使用下面的代码

RelativeLayout re = (RelativeLayout)findViewById(R.id.imageLayout); 
    re.setGravity(Gravity.CENTER); 
    for (int i = 0; i < rows; i++) { 
    for (int j = 0; j < cols; j++) { 
     final ImageView img = new ImageView(this); 
      //do some operations with image 
      RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(40,40); 
     if (j == 0) { 
        // rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 
        //   RelativeLayout.TRUE); 
      } 
      else { 
        rlp.addRule(RelativeLayout.RIGHT_OF, index - 1); 
       } 
     if (i == 0) { 
        // rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 
        //   RelativeLayout.TRUE); 
       } 
       else { 
        rlp.addRule(RelativeLayout.BELOW, index - cols); 
       }            

        img.setLayoutParams(rlp);    
       re.addView(img); 
     } 
     } 

图像以矩阵的形式添加,但视图对齐屏幕的左侧,但我希望矩阵位于屏幕的中心。

+0

我已经测试了'RelativeLayout'你上面的代码从上一个问题删除'ALIGN_PARENT_TOP'和'ALIGN_PARENT_LEFT'规则,它工作正常。所以你的代码中有其他的东西是有问题的。 – Luksprog

+0

好吧,我现在遇到了问题,我已经在Bootm中添加了两个文本浏览器,因此可能会导致问题。我认为这不会影响代码,所以我dint张贴在问题请看我编辑布局的代码,然后请告诉我的解决方案 – user990967

回答

0

正如我所说的,你可以在初始RelativeLayout,在包裹中另一个RelativeLayout图像矩阵和中心:

RelativeLayout re = (RelativeLayout)findViewById(R.id.imageLayout); 
RelativeLayout content = new RelativeLayout(this); 
RelativeLayout.LayoutParams clp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     clp.addRule(RelativeLayout.CENTER_IN_PARENT); 
     content.setLayoutParams(clp); 
for (int i = 0; i < rows; i++) { 
    for (int j = 0; j < cols; j++) { 
     // ...the current loop... 
     img.setLayoutParams(rlp);    
     content.addView(img); 
    } 
} 
re.addView(content); 

而且从XML RelativeLayout删除android:gravityandroid:layout_gravity性能。