Android 中的图像按钮
谁能告诉我如何调整 imageButton
的大小以完全适合图像?这是我尝试过的代码,但图像放置在我使用 android:scaleType
定位的位置,但我无法减小 imageButton
的大小。请帮助我纠正这个问题。我尝试的代码是:
<ImageButton>
android:id="@+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/eye"> // this is the image(eye)
</ImageButton>
Can anybody tell me how to resize the imageButton
to fit the image exactly? This is the code that I tried, but the image is placed at the position that I am locating using android:scaleType
, but I am not able to reduce the size of imageButton
. Please help me out in rectifying this issue. The code that I tried is:
<ImageButton>
android:id="@+id/Button01"
android:scaleType="fitXY" // i have tried all the values for this attr
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:paddingLeft="10dp"
android:src="@drawable/eye"> // this is the image(eye)
</ImageButton>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
自动工作。
这是我用来解决调整图像大小、按钮的宽度和高度的所有问题的方法......
works automatically.
was what I used with all the problems of resizing the image the the width and height of the button...
您正在使用属性“src”设置图像,
请使用“
background
”属性而不是“src
”属性:例如:
you are setting the image with the property "src"
use "
background
" property instead "src
" property:like:
您可能必须以编程方式调整按钮的大小。您需要在 onCreate() 方法中显式加载图像,并调整按钮的大小:
根据 setMinimumX 的规范,它不能保证正常工作(因为宽度和高度仍然取决于父视图),但是它应该适用于几乎所有情况。
You're probably going to have to resize the button programmatically. You'll need to explicitly load the image in your onCreate() method, and resize the button there:
It's not guaranteed to work, according to the specifications for setMinimumX (since the width and height are still dependent on the parent view), but it should work pretty well for almost every situation.
尝试使用 ScaleType centerInside。
ScaleTypes 未在 Eclipse 布局设计器中正确呈现,因此请在正在运行的应用程序中进行测试。
Try to use ScaleType centerInside.
ScaleTypes are not properly rendered in Eclipse Layout designer, so test in your running app.
您是否尝试像下面这样给出layout_width和layout_height?由于您使用wrap_content进行设置,因此图像按钮会扩展到源图像的高度和宽度的大小。
Did you try to give the layout_width and layout_height like the following? Since you are setting with wrap_content, the image button expands to the size of source image's height and width.
您不必使用 src 属性来使用它
错误的方式(图像不适合按钮)
正确的方式是使用背景属性
,其中 skin 是 xml
skin.xml
使用button_pressed.png 和button_normal.png
这也将帮助您创建具有按下、正常、禁用和聚焦四种状态的皮肤按钮。
确保所有 png 的大小保持相同
You don't have to use it using src attribute
Wrong way (The image won't fit the button)
Right way is to use background atttribute
where skin is an xml
skin.xml
using button_pressed.png and button_normal.png
This will also help you in creating your skinned button with 4 states of pressed , normal , disabled and focussed.
Make sure to keep same sizes of all pngs
您还可以将背景设置为透明。所以按钮看起来很适合你的图标。
You can also set background is transparent. So the button looks like fit your icon.
我认为您已经解决了这个问题,并且正如其他答案所建议的那样
。但我更喜欢它
,它也工作得很好。(当然,前面的代码会将图像设置为背景,另一个会将图像设置为图像)但根据你选择的答案,我猜你的意思是9-patch。
I think you already solved this problem, and as other answers suggested
is available. But I prefer
and it works well too.(of course former code will set image as a background and the other will set image as a image) But according to your selected answer, I guess you meant 9-patch.