动态创建的 ImageView 不遵守 RelativeLayout 规则

Dynamically created ImageView not respecting RelativeLayout rules

我正在将 ImageViews 动态添加到 RelativeLayout。我正在尝试将每个图像对齐到最后一个图像的右侧。初始图像正确对齐,但是当我将下一个图像设置为与上一个图像的右侧对齐时,它显示在屏幕的左上角,忽略了对齐规则。

Android 设备监视器中的层次结构 View 表明已正确设置 ID,并且 ImageView 识别出它应该与最后一个 ImageView 的 ID 对齐。

    List<String> image_urls = rally.getTransportationImgs();
    List<String> methods = rally.getTransportationStrs();
    ArrayList<ImageView> IMGS = new ArrayList<ImageView>();

    for(int i = 0; i < methods.size(); i++) {

        String method = methods.get(i);

        for(int j = 0; j < image_urls.size(); j++) {

            String img_url = image_urls.get(j);

            if(img_url.toLowerCase().contains(method.toLowerCase()) == true) {
                ImageView methodImage = new ImageView(this);

                methodImage.setId(j + 100);

                IMGS.add(methodImage);

                RelativeLayout detailsLayout = (RelativeLayout) findViewById(R.id.details_layout);
                TextView transportationText = (TextView) detailsLayout.findViewById(R.id.transportationText);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                if (IMGS.size() > 1) {

                    params.addRule(RelativeLayout.RIGHT_OF, IMGS.get(IMGS.size() - 1).getId());
                    params.addRule(RelativeLayout.ALIGN_TOP, IMGS.get(IMGS.size() - 1).getId());
                    params.addRule(RelativeLayout.END_OF, IMGS.get(IMGS.size() - 1).getId());

                    params.height = 5000;
                    params.width = 65;
                } else {

                    params.addRule(RelativeLayout.RIGHT_OF, transportationText.getId());
                    params.addRule(RelativeLayout.ALIGN_TOP, transportationText.getId());
                    params.addRule(RelativeLayout.END_OF, transportationText.getId());

                    params.height = 65;
                    params.width = 65;
                }


                methodImage.setLayoutParams(params);

                detailsLayout.addView(methodImage);

                Picasso.with(getApplicationContext()).load(img_url).fit().centerCrop().placeholder(R.drawable.ic_launcher).into(methodImage);
            }
        }
    }

XML 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:weightSum="1"
android:id="@+id/details_layout">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="275dp"
    android:id="@+id/image" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/name2"
        android:layout_below="@id/image"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/date2"
        android:layout_below="@id/name2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/creator_name2"
        android:layout_below="@id/date2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/venues"
        android:layout_below="@id/creator_name2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/transportationText"
        android:layout_below="@id/venues"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/categories"
        android:layout_below="@id/transportationText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

出于调试目的,我将图像高度设置得过大。灰色是图像的一部分,应该位于 ImageView 的右侧,正确对齐 "Transportation:"

这一行有问题:

methodImage.setId(j + 100);

如果 methods 数组中有多个 Object,则 ImageViews id 会发生冲突。您仅根据内部数组 (j) 的索引生成 ID。

例如如果:

  1. methods
  2. 中有两项
  3. image_urls中有三项

您的 ImageViews 将具有 ID:[101, 102, 103, 101, 102, 103]

因此您将添加多个具有相同 ID 的 Views

要解决此问题,要么基于 ij 生成 ids,要么在更多 conventional way.

中生成它们

编辑: 好吧,另一个问题是你试图设置 RelativeLayout Rules 将特定的 View 对齐到相同的 View.

看看你的代码:

您正在创建一个 ImageView,设置它的 id 并立即将它添加到 IMGS 列表,其中:

IMGS.add(methodImage);

然后您尝试设置 RelativeLayout Rules,这应该与您的 ImageView 对齐,但您引用的是 last[= IMGS 列表中的 83=] 项,其中:

IMGS.get(IMGS.size() - 1)

与数组中的最后一项相同View,您正在尝试设置!

解决问题:

a) 将IMGS.add(methodImage);移到if-else之后,将if条件改为if (IMGS.size() > 0) {

b) 将 if 正文更改为:

params.addRule(RelativeLayout.RIGHT_OF, IMGS.get(IMGS.size() - 2).getId());
params.addRule(RelativeLayout.ALIGN_TOP, IMGS.get(IMGS.size() - 2).getId());
params.addRule(RelativeLayout.END_OF, IMGS.get(IMGS.size() - 2).getId());