【问题标题】:LinearLayout ImageView Resize All Image Widths To FitLinearLayout ImageView 调整所有图像宽度以适合
【发布时间】:2013-02-17 19:08:54
【问题描述】:

我有一个包含 3 个图像视图的水平线性布局。每个图像视图都包含相同的图像,即 200x200 像素。这是我的布局 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image200" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image200" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image200" />

    </LinearLayout>

</RelativeLayout>

这是它的样子:

注意我假设从左边数第三张图片的大小是如何调整的,因为没有足够的空间来容纳 3x 200 像素宽的图片。

我宁愿做的不是只缩小最后一张图片,而是均匀调整所有三张图片的大小,以便它们都适合整个屏幕。如何设置布局以均匀地适合所有三个图像?

谢谢

更新 - 更改我的设置后,这里的情况如下:

请注意,图像视图周围的 边框 仍然是 200 像素高。为什么会这样?

【问题讨论】:

    标签: android imageview width android-linearlayout


    【解决方案1】:

    将图像视图的宽度和高度设置为match_parent,并为所有图像视图设置 layout_weight=1。同时设置属性android:scaleType="fitXY"

    【讨论】:

    • android:scaleType="fitXY" 没用,因为 layoutHeight 处于包装内容模式
    • 它像我上面所说的那样工作,但问题是它是缩放图像的宽度而不是高度,从而使图像纵横比变得一团糟。删除 fitXY 会导致它调整两个维度的大小。谢谢!
    • @JanTacci 我想你也想拉伸你的图像高度,所以我建议使用 scaleType
    • 我也想拉伸高度(以保持纵横比),但 fitXY 似乎不起作用。当我删除 fitXY 但保留了您的其余建议时,它起作用了
    • 我已经更新了我的原始帖子,并附上了应用建议后的截图。但是请注意,即使图像已经缩小,实际布局高度仍然是 200 像素。为什么会这样?
    【解决方案2】:

    这对我来说非常有效:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:adjustViewBounds="true"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
        <ImageView
            android:adjustViewBounds="true"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
        <ImageView
            android:adjustViewBounds="true"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

    当设置为 0dp 时,布局宽度就是画面中的布局宽度。此处 LinearLayout 的布局宽度与父级匹配。所以它会占用整个宽度,并且根据图像的布局宽度,它们会按照重量的比例占用空间。

    【讨论】:

    • 请解释一下你的答案!
    • @Mazz:添加了一个简短的解释。如果还有什么令人困惑的地方,请告诉我。
    【解决方案3】:

    @JanTacci 关于您的后续问题“但是请注意,即使图像已缩小,实际布局高度仍为 200 像素。为什么会发生这种情况?”

    我刚刚遇到了同样的问题,并通过将这个属性添加到每个 ImageView 来修复它:

    android:adjustViewBounds="true"
    

    【讨论】:

      【解决方案4】:

      让你的三张图片宽度为 0dp

      并为它们添加 1 的 layout_weight

      ;)

      <ImageView
                  android:id="@+id/imageView1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:src="@drawable/image200" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-31
        • 2013-04-27
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多