我创建了一个RelativeLayout
,我希望根据自己的喜好调整9张Imageviews。我首先尝试在 XML 中这样做但是因为边距会根据不同的屏幕大小而改变,我认为通过代码执行它会更容易。它们应该看起来像一个3X3方形矩阵,这样第一个图像视图就会有2到2和2。
我做了什么: -
首先在XML文件中初始化imageview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pagefw" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"
android:layout_marginTop="142dp"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_toLeftOf="@+id/imageView2"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView4"
android:layout_toRightOf="@+id/imageView4"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView3"
android:layout_alignTop="@+id/imageView5"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView4"
android:layout_below="@+id/imageView4"
android:layout_marginTop="14dp"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView7"
android:layout_toLeftOf="@+id/imageView6"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView6"
android:layout_alignTop="@+id/imageView8"
android:minHeight="80dp"
android:minWidth="80dp"
android:src="@drawable/cross" />
</RelativeLayout>
然后我按代码更改了第一个Imageview的位置。
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(iv1.getLayoutParams());
lp.setMargins(left, top, right, bottom);
iv1.setLayoutParams(lp);
然后根据第一个图像视图对齐其余的图像视图。
RelativeLayout.LayoutParams sec = new RelativeLayout.LayoutParams(iv2.getLayoutParams());
sec.addRule(RelativeLayout.ALIGN_TOP, R.id.imageView1);
sec.addRule(RelativeLayout.ALIGN_RIGHT, R.id.imageView1);
iv2.setLayoutParams(sec);
RelativeLayout.LayoutParams thr = new RelativeLayout.LayoutParams(iv3.getLayoutParams());
thr.addRule(RelativeLayout.ALIGN_TOP, R.id.imageView1);
thr.addRule(RelativeLayout.RIGHT_OF, R.id.imageView2);
iv3.setLayoutParams(thr);
RelativeLayout.LayoutParams fou = new RelativeLayout.LayoutParams(iv4.getLayoutParams());
fou.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.imageView1);
iv4.setLayoutParams(fou);
我尝试使用前4个imageview来查看它是否有效,但事实并非如此。
我得到的是什么: - 只有第一个imageview出现在其他地方出现的确切位置。
所以什么是错的。提前谢谢。
答案 0 :(得分:0)
试试这个:
RelativeLayout.LayoutParams sec = new RelativeLayout.LayoutParams(iv2.getLayoutParams());
sec.addRule(RelativeLayout.ALIGN_TOP, R.id.imageView1);
sec.addRule(RelativeLayout.RIGHT_OF, R.id.imageView1);
iv2.setLayoutParams(sec);
RelativeLayout.LayoutParams thr = new RelativeLayout.LayoutParams(iv3.getLayoutParams());
thr.addRule(RelativeLayout.ALIGN_TOP, R.id.imageView1);
thr.addRule(RelativeLayout.RIGHT_OF, R.id.imageView2);
iv3.setLayoutParams(thr);
RelativeLayout.LayoutParams fou = new RelativeLayout.LayoutParams(iv4.getLayoutParams());
fou.addRule(RelativeLayout.BELOW, R.id.imageView1);
iv4.setLayoutParams(fou);