RelativeLayout忽略了第二个ImageView?

时间:2012-10-16 08:13:11

标签: android android-layout

我有以下XML布局

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical">

            <ImageView
                android:id="@+id/IVLEFT"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:background="#ff0000"
                android:padding="1dp"
                />

            <ImageView
                android:id="@+id/IVRIGHT"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_toRightOf="@+id/IVLEFT"
                android:layout_alignParentRight="true"
                android:layout_alignBottom="@+id/IVLEFT"
                android:layout_alignTop="@+id/IVLEFT"
                android:src="@drawable/shadow_pages"
                android:background="#00ff00"
                android:padding="1dp" />
    </RelativeLayout>

我希望我的布局有2个ImageViews - 一个在左边,一个在右边。 但实际发生的是:

在运行时我将IVLEFT的图像设置为大图像(比如1024X768) - 它覆盖了我的所有布局,放弃了IVRIGHT(我甚至看不到它)

有什么想法吗?

4 个答案:

答案 0 :(得分:0)

这是因为您已将ImageView宽度和高度设为fill_parent。因此它将获得整个布局。此外,您的布局取决于您的第一张图片,因为如果您将大图片提供给第一张Imageview,则会占用整个布局。

如果您想并排展示2张图片,请尝试linearlayoutlayout_weightsimageViews一起使用。

同时将RelativeLayout宽度和高度设为fill_parent

答案 1 :(得分:0)

这是因为您的RelativeLayout宽度为wrap_content。它占据了屏幕之外所需的所有空间。

改为使用fill_parent

答案 2 :(得分:0)

  1. 将RelativeLayout宽度更改为fill_parent

  2. xml中没有带id = "@+id/magazineImageView"的ImageView,如果你的意思是"@+id/IVLEFT",那么第一张图片本身占据整个屏幕宽度,因此你看不到第二张图片。

  3. 如果您想看到第二张图片同时将ImageView宽度设为wrap_content或某些硬编码值

  4. 更重要的是,当您使用任何View ID作为参考时,请不要在ID中使用 + 签名。 + 符号首次用于定义视图的ID

  5. 使用:

    android:layout_toRightOf="@id/magazineImageView"
    

答案 3 :(得分:0)

试试这个::

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical">

            <ImageView
                android:id="@+id/IVLEFT"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="#ff0000"
                android:padding="1dp"
                android:src="@drawable/ic_launcher"/>

            <ImageView
                android:id="@+id/IVRIGHT"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/IVLEFT"
                android:layout_alignParentRight="true"
                android:layout_alignBottom="@+id/IVLEFT"
                android:layout_alignTop="@+id/IVLEFT"
                android:src="@drawable/ic_launcher"
                android:background="#00ff00"
                android:padding="1dp" />
    </RelativeLayout>
相关问题