在我的图像上额外填充

时间:2013-03-16 22:32:42

标签: android whitespace android-linearlayout

我有一个LinearLayout(水平),我用它来显示3个ImageViews。当我第一次添加ImageViews时,最后一个缩小,因为图像太大而不适合3。这是我的设计师屏幕的样子:

after initially adding the three images

请注意第三张图片缩小了。

要解决此问题,请在每个ImageView上设置以下属性。

width: 0dp
weight: 1

以下是设置这些属性后我的图片的外观:

after setting attributes

我遇到的问题是即使图像已经收缩,顶部和底部仍然有空间,如下图中的蓝色矩形所示:

padding

我的问题是,为什么这个额外的填充在那里,我怎么能摆脱它?

谢谢

更新 - 添加我的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@color/app_background_color"
android:contentDescription="@string/game_image_button"
android:gravity="center"
tools:context=".EasyActivity" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="@string/game_image_button"
        android:src="@drawable/ruler200x200" />

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="@string/game_image_button"
        android:src="@drawable/ruler200x200" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:contentDescription="@string/game_image_button"
        android:src="@drawable/ruler200x200" />
</LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

ImageViews支持不同的ScaleTypes,默认为FIT_CENTERhere是其他选项。 FIT_CENTER表示它的高度和宽度与您的图像相同(在您添加重量之前,在第一个屏幕截图中图像的样子)。我相信发生的是它然后根据你的layout_weight调整图像的宽度,并通过缩小图像的宽高比来适应新的宽度。它将高度保持为原始高度并使结果居中。

我认为其他比例类型之一会做你想要的。可能是CENTERCENTER_INSIDE

答案 1 :(得分:0)

您可以使用以下代码

<Imageview.....
android:adjustViewBounds="true"
..../>