Centering the Matrix of ImageViews to the Center of the Screen Dynamically

爷,独闯天下 提交于 2019-12-14 03:34:15

问题


This is my Layout File:

 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>

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.


回答1:


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

Also remove the android:gravity and the android:layout_gravity properties from the xml RelativeLayout.



来源:https://stackoverflow.com/questions/18526772/centering-the-matrix-of-imageviews-to-the-center-of-the-screen-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!