两个ImageView并排使用不同的图像尺寸,但希望保持相同的宽高比和尺寸

时间:2012-11-15 20:22:51

标签: android android-layout android-activity android-imageview android-image

这已经困扰了我一段时间,我不知道如何解决它。

假设我有以下布局:2个ImageViews,每个ImageView下都有一个TextView。远程检索图像。我希望每个ImageView占用它们所在活动宽度的大约40%,其下面有空白区域。 TextViews将缩小约5%,位于每个ImageView下方。

我尝试了什么:

将每对ImageView和TextView放在他们自己的LinearLayout中,所以2个LinearLayouts,每个LinearLayout的layout_width设置为fill_parent,并且两者的layout_weight(0.5)相等。 每个ImageView的layout_width为fill_parent,layout_margin为15dip。 layout_height设置为wrap_content(这是我不确定如何设置的参数)。

这很好但问题是所有传入的图像都有不同的大小和比例,所以它们看起来很奇怪。

我继续设置: 机器人:scaleType = “centerCrop” 机器人:adjustViewBounds = “真”

这有所帮助,但有时每个ImageView的大小仍然不同。在这一点上,我只是将每个ImageView放在固定宽度100下左右 两个ImageViews,这有帮助,但当然,对于更大尺寸的设备,这将看起来很奇怪。我怎样才能实现我想要完成的目标?

1 个答案:

答案 0 :(得分:1)

在第二个选项中,尝试不使用" AdjustViewBounds"记住:

  

如果希望ImageView调整其边界,请将此项设置为true   保留其可绘制的宽高比。

但这是我在你的案子中会做的事情:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scaleType="center"
        android:src="@drawable/99" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scaleType="center"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="TextView" />
</LinearLayout>

它会在更大的屏幕上显示:

Test1

在小屏幕上:

test2

在这种情况下,我更喜欢中心,因为如果你的照片较小,那么它们看起来会更好。如果可以的话,我尽量避免使用固定尺寸,因为正如你所说,它在大型设备上看起来不太好看。

您还可以为每种尺寸制作不同的布局。但你必须保留所有这些。检查Supporting Multiple Screen Sizes(如果您还没有;)。