安卓ImageButton不要拉伸图片

5
如何防止ImageButton拉伸图片?
使用以下代码片段:
 <ImageButton
                android:src="@drawable/prediction"
                android:background="?android:attr/selectableItemBackground"
                android:gravity="center_horizontal"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:contentDescription="some description"
                android:text="@string/predict"
                local:MvxBind="Click ExecuteQueryCmd" />

我得到以下效果:

enter image description here

图像为64x64的png格式。

我想要的是ImageButton能够尊重所分配的图像的本地宽度和长度,因此分辨率。基于类似的帖子的建议是使用Button,将其背景设置为图像,然后使用自定义选择器,但这听起来像是一种hack方法。或者这是正确的方法吗?


你已经将其高度和宽度设置为wrap-content,但它仍然被拉伸了吗?看起来像是一个奇怪的问题。 - Illegal Argument
你检查了scaleType = "center"吗? - Gaurav Vashisth
我在我的应用程序中经常使用图像按钮。看到你的问题后,我检查了我的应用程序,发现它并不存在。我使用带有透明背景的PNG图像。 - Illegal Argument
使用透明的PNG图片时,我也没有遇到任何问题。背景图片会被拉伸,但是在src属性中使用的图像保持不变。 - JanBo
@Subash,你的图片没有被拉伸吗?如果是这样,那么承载这些ImageButtons的父容器是什么? - Klaus Nji
显示剩余3条评论
3个回答

9

这对我很有用,使用以下方法

  <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:contentDescription="some description"
    android:scaleType="centerInside"
    android:src="@drawable/ci_icon"
    android:text="@string/predict"
    local:MvxBind="Click ExecuteQueryCmd" />

将您的图片放置在 android:src 属性中。

刚才尝试了一下,图片还是拉伸的。真让人沮丧啊,在Android上做这么简单的事情竟然这么困难。另外请注意,我不能使用"@android:color/transparent",因为按钮在被点击后不再改变颜色。 - Klaus Nji

0
做这个:
<ImageButton
                android:src="@drawable/prediction"
                android:background="?android:attr/selectableItemBackground"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:contentDescription="some description"
                android:text="@string/predict"
                local:MvxBind="Click ExecuteQueryCmd" />

0
将ImageButtons的宽度设置为fill_parent,并对贴着左边缘的图像使用fitStart的比例类型,对于右侧的图像则使用fitEnd。这应该可以解决你的问题,至少对于你的示例图像而言是如此。如果图像的比例宽度超过屏幕宽度,则可能会出现一些间距问题,但对你来说应该可以工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接