【问题标题】:Scale all ImageViews in LinearLayout to suit LinearLayout Width缩放 LinearLayout 中的所有 ImageView 以适应 LinearLayout 宽度
【发布时间】:2014-06-18 23:37:00
【问题描述】:

我有一个包含 5 个图像视图的水平线性布局。每个图像视图的宽度不同,但高度相同。 在横向看起来很好(下图 1),但是当我将线性布局调整为纵向时,最后一个图像无法适应,而倒数第二个被缩放以适应(下图 2)。 我想要的是所有图像都可以缩放以适应线性布局(下图 3)。

这可能吗?

也许有一种方法可以创建固定纵横比的LinearLayout?这样,图像将调整到 LinearLayout 的高度,LinearLayout 的高度将调整以保持与 LinearLayout 的宽度的纵横比,LinearLayout 的宽度将调整以适应屏幕。

布局将是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="horizontal">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/image_01">
</ImageView>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/image_02">
</ImageView>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/image_03">
</ImageView>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/image_04">
</ImageView>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/image_05">
</ImageView>

</LinearLayout>

</RelativeLayout>

对每个图像使用 android:layout_weight="1" 不起作用。我明白了:

【问题讨论】:

  • 你能发布你的布局文件吗?

标签: android imageview width android-linearlayout


【解决方案1】:

做这样的事情

<LinearLayout android:weightSum="5" >

        <ImageButton
            android:id="@+id/image1"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/image2"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/image3"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/image4"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/image5"
            android:layout_weight="1" />
    </LinearLayout>

【讨论】:

    【解决方案2】:

    根据您的要求将尺寸设为静态

    试试这个:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="horizontal"
            android:weightSum="5" >
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/image_01" >
            </ImageView>
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/image_02" >
            </ImageView>
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/image_03" >
            </ImageView>
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/image_04" >
            </ImageView>
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/image_05" >
            </ImageView>
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 对所有人使用类似 100dp 的宽度和高度会得到相同的东西,其中所有的宽度都相同。如果我给出每个图像的宽度和高度的实际像素,我会得到一个奇怪的结果,其中最小宽度的图像被丢弃以允许其他图像部分适合。