删除ImageButton的填充 [英] Remove padding of ImageButton

查看:65
本文介绍了删除ImageButton的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得一个 ImageButton ,它具有固定的高度,并且根据其源图像的比例仅根据需要而变宽?

How can I get an ImageButton that has a fixed height and is only as wide as it needs according to the ratio of its source image?

我尝试了以下操作:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="8pt"
    android:minWidth="0pt"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@drawable/list_download"
    android:background="@android:color/black"
    android:padding="0pt"/>

我将背景设置为黑色只是为了确切地看到其边界,然后在此处是结果.

I set background to black just to see the boundaries of it exactly, and here is the outcome.

我已经看到一些建议使用负填充的解决方案,但这不是一个很好的解决方案.应该有更好的一个.

I have seen some solutions that suggest to use negative paddings but it is not an elegant solution. There should be a better one.

我也在 ImageView 上尝试了类似的配置,但是它也有额外的填充.因此,这不是特定于 ImageButton 的问题(即,它与九补丁问题无关).

I tried similar configurations on an ImageView too, but it also had extra padding. So it's not an ImageButton specific problem (i.e. it is not related to this nine-patch issue).

如果我将 scaleType fitCenter 更改为 fitStart ,那么结果就像

If I change scaleType from fitCenter to fitStart, then the outcome is like this. So there is somehow a minimum width.

推荐答案

这是因为您设置的高度有限.这限制了图像的高度,但是当Android计算内容的宽度(原始未缩放的图像)时,它的宽度会变宽.

It's because you've set a limited height. That limits the image height, but when Android calculates the width of the content (the original unrezised image), it's wider.

如果还使用wrap_content作为高度,则将获得侧面没有黑色填充"的图像.

You'll get the image without the black "padding" on the side if you use wrap_content for the height as well.

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/list_download"
android:background="@android:color/black"
android:padding="0pt"/>

因此,如果您希望图片具有特定的尺寸,则可以在可绘制文件夹中将其调整为所需的尺寸.

So, if you want the picture to be a specific size, you could resize it in your drawable folder to the size you want.

这篇关于删除ImageButton的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆