LinearLayout ImageView调整所有图像宽度以适应

Jan*_*cci 6 android width imageview android-linearlayout

我有一个包含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>
Run Code Online (Sandbox Code Playgroud)

这是它的样子:

线性布局测试截图

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

我宁愿发生的不是仅缩小最后一张图像,而是将所有三张图像均匀调整大小以使它们都适合整个屏幕.如何设置我的布局以均匀地适合所有三个图像?

谢谢

更新 - 在更改我的设置后,这是什么样的事情:

更新

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

Pra*_*ani 14

match_parent为图像视图设置宽度和高度,并为所有图像视图设置layout_weight = 1.还设置属性android:scaleType="fitXY"


bla*_*ack 7

这对我来说非常有效:

    <?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>
Run Code Online (Sandbox Code Playgroud)

布局宽度设置为 0dp 时,布局宽度就是图片中的内容。这里 LinearLayout 的布局宽度是匹配父级的。所以它将占用整个宽度,并且根据图像的布局宽度,它们按重量的比例占用空间。