【问题标题】:Centering the Matrix of ImageViews to the Center of the Screen Dynamically将 ImageViews 矩阵动态居中到屏幕中心
【发布时间】:2013-08-30 06:55:42
【问题描述】:

这是我的布局文件:

 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 的 about 布局中

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

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

【问题讨论】:

  • 我已经测试了上面的RelativeLayout,我的代码从上一个问题中删除了ALIGN_PARENT_TOPALIGN_PARENT_LEFT 规则,它工作正常。因此,您的代码中还有其他问题。
  • 好的,我现在遇到了问题,我在 Bootm 上添加了另外两个文本视图,所以这可能是导致问题的原因。我认为这不会影响代码,所以我将其发布在问题中请参阅我正在编辑布局的代码,然后请告诉我解决方案

标签: android android-imageview android-relativelayout


【解决方案1】:

正如我所说,您可以将图像矩阵包装在另一个 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 属性。

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2017-10-22
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    相关资源
    最近更新 更多