围绕ImageViews的矩阵屏幕的中心动态 [英] Centering the Matrix of ImageViews to the Center of the Screen Dynamically

查看:223
本文介绍了围绕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使用以下code中的有关版面

I am adding the ImageViews Dynamically to the about layout named MatchGame.xml Using the following code

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

的图像添加在矩阵的形式,但视图排列在屏幕的左侧,但我想矩阵是在屏幕的中心
请帮我它非常迫切
在此先感谢!

The Images are Added in the form of the matrix but the Views are aligned to the left of the Screen But I want the Matrix to be at the center of the screen Please Help me Its very Urgent Thanks in advance!

推荐答案

正如我所说的,你可以在最初,在包装中的另一个图像矩阵 RelativeLayout的和中央 RelativeLayout的

As I said you could wrap the image matrix in another RelativeLayout and center that in in the initial 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);

另外删除安卓重力的android:从XML layout_gravity 属性 RelativeLayout的

这篇关于围绕ImageViews的矩阵屏幕的中心动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆