【问题标题】:Unable to Align ImageView by code无法通过代码对齐 ImageView
【发布时间】:2014-03-16 14:59:04
【问题描述】:

我创建了一个RelativeLayout,我想根据自己的喜好在其中对齐 9 个 Imageview。我第一次尝试在 XML 中这样做,但是因为边距会根据不同的屏幕尺寸而变化,我认为通过代码来做会容易得多。它们应该看起来像一个 3X3 方阵,因此第一个图像视图将有 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);

然后根据第一个imageview对齐其余的imageview。

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 出现在其他地方出现的确切位置。

那怎么了。提前致谢。

【问题讨论】:

标签: android xml android-layout


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2011-06-04
    • 2017-09-23
    • 2018-05-09
    • 2011-09-05
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    相关资源
    最近更新 更多