如果使用“fill_parent”或“weight”拉伸视图或按钮,如何防止我的位图在 ImageView 或 ImageButton 中自动缩放?
这将很有用,例如,在屏幕顶部创建一个 4 按钮工具栏,其中按钮等间距,但即使我使用 scaleType="center",按钮内的图像也会不断拉伸,这应该可以防止根据文档进行缩放,但事实并非如此。
任何见解表示赞赏!
谢谢,
如果使用“fill_parent”或“weight”拉伸视图或按钮,如何防止我的位图在 ImageView 或 ImageButton 中自动缩放?
这将很有用,例如,在屏幕顶部创建一个 4 按钮工具栏,其中按钮等间距,但即使我使用 scaleType="center",按钮内的图像也会不断拉伸,这应该可以防止根据文档进行缩放,但事实并非如此。
任何见解表示赞赏!
谢谢,
我发现使用 android:background 时会自动将您正在使用的图像缩放到视图的大小。
我尝试使用 android:src 将图像放在视图中。图像没有缩放,但我无法让图像相对于视图的大小居中。
所以我尝试将整个 Button 设为它自己的相对布局,这就是我使用的:
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageButton android:id="@+id/ImageButton01"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageButton>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cardback1"
android:layout_centerInParent="true"></ImageView>
</RelativeLayout>
ImageButton 位于背景中,并且始终与布局大小相同。然而,ImageView 将始终保持在 RelativeLayout 的中心,并且不会缩放。这样,RelativeLayout 本身可以增长和移动,并且按钮顶部的 Image 将始终保持相同的大小,但按钮会增长。但是,如果布局变得小于图像本身,图像将缩小。
我想这就是你要找的。可能有更好的方法来做到这一点,但这是我现在能想到的。
只需修改您的可绘制对象,应用更大的透明背景。假设我在 hdpi 中的可绘制对象是 41*48 px icon.png 。我创建了一个具有透明背景的 60*60 像素的新图像,复制粘贴以前的 icon.png 并以相同的名称保存。这样你就不需要触摸你的布局。
然后,如果您应用非透明 android:background ,则可以检查按钮区域是否更大:
<ImageButton android:id="@+id/widget_open"
android:src="@drawable/icon_widget_arrow_up"
android:background="#ff777777"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageButton>
将以前的和新的drawable应用到android:src,你应该清楚地看到 ImageButton 区域。
顺便说一句,我正在以兼容模式运行该应用程序。
如果图像被缩放,请确保您没有在兼容模式下运行您的应用程序(例如,如果您的目标是 Android 1.5/1.6 而不支持多密度,并且您在 Android 2.0 上运行该应用程序。)