在android中的xml中创建缩放图像

时间:2012-08-20 19:58:30

标签: android scale android-linearlayout

我要将5张图像放在水平LinearLayout内的图像按钮中。图像在磁盘上以不同的大小保存,但是当放置在布局中时,我希望它们具有相同的高度,但是具有不同的权重0.5,1,1,1,0.5(意味着布局的重量总和)是4)。我尝试使用此代码,但它不起作用:

 <LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="4" >

    <ImageButton
        android:id="@+id/scrollLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/left"
        android:layout_weight="0.5" />

    <ImageButton
        android:id="@+id/dog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/dog"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/fish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fish"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/cat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cat"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/scrollRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/right"
        android:layout_weight="0.5" />

</LinearLayout>

我是否可以更改layout_widthlayout_height的任何参数以使其生效,或者我是否需要为每个按钮从java代码创建缩放位图? 谢谢!

1 个答案:

答案 0 :(得分:0)

尝试定义相同的高度,然后缩放以保持比率

<ImageView
    .... 
    android:layout_height="match_parent"
    android:scaleType="centerInside"
    .... />
使用match_parent作为高度的

编辑会使ImageButtons具有相同的高度并使用scaleType保持比例比率