0

这一直困扰着我一段时间,我不知道如何解决它。

假设我有以下布局:2 个 ImageView,每个 ImageView 下都有一个 TextView。正在远程检索图像。我希望每个 ImageView 占据它们所在的 Activity 宽度的大约 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 (这是我不确定如何设置的参数)。

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

我继续设置: android:scaleType="centerCrop" android:adjustViewBounds="true"

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

4

1 回答 1

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>

它会在更大的屏幕上看起来像:

测试1

在较小的屏幕上:

测试2

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

您还可以为每个屏幕尺寸制作不同的布局。但是您必须保留所有这些。检查支持多种屏幕尺寸(如果您还没有;))。

于 2012-11-15T22:00:28.657 回答