8

我有

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:id="@+id/ImageView01"
        android:src="@drawable/phone80"></ImageView>
    <ImageView android:id="@+id/ImageView02"
        android:layout_height="wrap_content" android:src="@drawable/underline"
        android:layout_width="fill_parent" android:layout_gravity="fill_vertical|fill_horizontal"></ImageView>
</LinearLayout>

第一个图像是一个固定大小的图像,根本不应该缩放。其右侧的图像应水平缩放到最大空间。源指向一个有效的 .9.png 文件。不幸的是,它总是只显示其原始大小。我必须设置什么属性?ImageView 是正确的对象吗?

谢谢。

4

4 回答 4

55

只需添加android:scaleType="fitXY"到您的ImageView. 例如对于 myimage.9.png:

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/myimage"
    android:scaleType="fitXY" />
于 2012-08-17T12:19:49.083 回答
22

改为使用android:background。如果它只是一个下划线,你可以使用 aView而不是 a ImageView。至于填充屏幕的其余部分,您可能想要使用 aTableLayout代替。

另外,请确保您的 9-patch 是正确的。1px 边必须是黑色的。#000

于 2011-03-09T08:03:13.420 回答
1

还要检查以确保侧面的比例线不会一直延伸到图像的角落。我知道那里有黑色像素真的没有意义,但我认为 Android 会忽略它们,但它实际上阻止了缩放工作。它还在输出窗口中为您提供“错误:9-patch image xxx.9.png malformed”消息,但您可能没有注意到这一点。一旦我取出角落中的像素,它就开始为我工作。

于 2013-10-30T18:08:38.860 回答
0

尝试使用 setImageResource(9patchResourceID) 而不是 setImageBitmap 或 setImageBackground 并且它可以工作:)

于 2015-10-16T07:36:01.050 回答