在LinearLayout中缩放所有ImageView以适应LinearLayout宽度

时间:2014-05-05 12:38:30

标签: android imageview width android-linearlayout

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

这可能吗?

也许有一种方法可以创建固定宽高比LinearLayout?这样,图像将调整为LinearLayout的高度,LinearLayout的高度将根据LinearLayout的宽度进行调整,以适应屏幕的宽度。

Example Images

布局将是:

<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”不起作用。我明白了:

Example Images

2 个答案:

答案 0 :(得分:0)

做这样的事情

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

答案 1 :(得分:0)

根据您的需求确定尺寸静态

试试这个:

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