Android ImageButton -- 无法在中心获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/660629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ImageButton -- can't get image in the center
提问by executor21
I want to create an Android ImageButton in the xml file with a background image, and a smaller icon on top of that, right in the center. For some reason, it's not obvious how to do it, and documentation is no help.
我想在 xml 文件中创建一个带有背景图像的 Android ImageButton,并在其顶部创建一个较小的图标,就在中心。出于某种原因,如何做到这一点并不明显,文档也无济于事。
<ImageButton
android:id="@+id/sound_button"
android:layout_x="430px"
android:layout_y="219px"
android:layout_width="48px"
android:layout_height="48px"
android:scaleType="center"
android:src="@android:drawable/volumeicon"
android:background="@drawable/clearbuttonup"
/>
However, no matter what I try (putting "wrap _ content" instead of absolutenumbers on layout _ height and layout _ width, the icon remains in the top left corner of the button. If, however, I have no background image (i.e. a system default white button), the icon does go into the center. The button also goes into the top left corner when I use a color instead of an image for the background.
但是,无论我尝试什么(在布局 _ 高度和布局 _ 宽度上放置“ wrap _ content”而不是绝对数字,图标仍保留在按钮的左上角。但是,如果我没有背景图像(即一个系统默认的白色按钮),图标确实进入中心。当我使用颜色而不是图像作为背景时,按钮也会进入左上角。
Why does this happen, and how would I actually get the behavior I want-- that is, a background image with the icon in the center?
为什么会发生这种情况,我如何才能真正获得我想要的行为 - 即带有中心图标的背景图像?
采纳答案by executor21
I'm a real moron. That's how it appears in the display the Ecliplse plugin provide side-by-side with the XML editor. When I actually build the project and ran it on a device, it displayed everything correctly.
我真是个白痴。这就是它在 Ecliplse 插件与 XML 编辑器并排提供的显示中的显示方式。当我实际构建项目并在设备上运行它时,它正确显示了所有内容。
回答by Alnitak
Ironically I had exactly the opposite problem with an ImageView - I couldn't get it left-aligned no matter what I tried. In this case I was using layout_width="fill_parent"
.
具有讽刺意味的是,我在 ImageView 上遇到了完全相反的问题 - 无论我尝试什么,我都无法将其左对齐。在这种情况下,我使用layout_width="fill_parent"
.
See my question here: Android layout - alignment issue with ImageView
在此处查看我的问题:Android 布局 - ImageView 对齐问题
回答by Oubaida AlQuraan
try use the Button and set the background it's very useful and easy :D
尝试使用按钮并设置背景,它非常有用且简单:D
make the text empty and you can use it like image button ^_^
使文本为空,您可以像图像按钮一样使用它^_^