【问题标题】:Two ImageViews side by side with different image sizes but wanting to maintain same aspect ratio and sizes两个具有不同图像大小但希望保持相同纵横比和大小的 ImageViews
【发布时间】:2012-11-04 12:38:02
【问题描述】:

这个问题困扰了我一段时间,我不知道如何解决它。

假设我有以下布局:2 个 ImageView,每个 ImageView 下都有一个 TextView。正在远程检索图像。我希望每个 ImageView 占据它们所在的 Activity 宽度的大约 40%,并在它们下方留有空白。 TextView 将缩小约 5%,位于每个 ImageView 下方。

我尝试了什么:

将每对 ImageView 和 TextView 放在各自的 LinearLayout 中,即 2 个 LinearLayout,每个 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 放在固定宽度 100 dip 左右 对两个 ImageViews 都有帮助,但当然对于更大尺寸的设备,这看起来很奇怪。我怎样才能实现我想要实现的目标?

【问题讨论】:

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


    【解决方案1】:

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

    如果您希望 ImageView 将其边界调整为 保留其可绘制对象的纵横比。

    但在你的情况下,我会这样做:

    <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>
    

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

    在较小的屏幕上:

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

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

    【讨论】:

    • 谢谢,但是图像之间的大小差异并不是很大,假设最小的 png 和最大的 png 之间最大差异为 60 像素。如果尺寸差异像上面那样很大,则居中看起来不错。但是,如果它是 20 像素的差异,图像并排看起来不会很好。我在此评论中使用像素来描述图像大小的不同,而不是它们的布局方式(所以没有人说使用 DIP!)
    • 好吧略有不同...你能解释一下“有时大小仍然不同”,什么时候?也许使用:layout_height = match_parent?并将权重放在线性布局内的 ImageView 上,而不放在 textView 上?离开中心作物...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多