为什么LinearLayout中第二个相同的ImageView更小?

时间:2014-10-12 11:04:31

标签: android imageview android-linearlayout

我有两个ImageView,在LinearLayout中有相同的图像源,但为什么第二个图像小于第一个?

这是源代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/background_landscape" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/background_landscape" />

    </LinearLayout>
</RelativeLayout>

Screenshot ImageView with smaller image on the second image

可以在800x1280像素分辨率下清晰地再现错误。

如何在屏幕右侧裁剪第二张图像时使两张图像的大小相同。 (不缩小)

2 个答案:

答案 0 :(得分:1)

  

...为什么第二张图像小于第一张图片?

因为wrap_content属性的layout_width值导致第一张图片的真实宽度。并且第二个图像从LinearLayout接收静止宽度(布局宽度减去第一个图像的宽度),这明显小于图像的实际宽度。这就是它缩小的原因。

  

如何使两个图像与第二个图像具有相同的大小   裁剪在屏幕的右侧。 (不缩小)

您可以使用ScrollView,这允许孩子离开父母的界限。

更新:如CommonsWare所述:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="false"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/background_landscape" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/background_landscape" />

    </LinearLayout>

</HorizontalScrollView>

答案 1 :(得分:0)

与beworker一样,第一张图片由wrap_content获得宽度,第二张图片获得剩余的可用水平空间。 如果您希望每个android:layout_weight="1"占用可用屏幕宽度的一半,则ImageViews使用{{1}}