创建一个带有自定义布局android的按钮
图片注释:图标应居中在红色位。文本应位于黄色位的中心。
我正在尝试在 android 中创建一个看起来与上面非常相似的按钮。我尝试使用图像按钮,普通按钮(带有drawabletop和drawablebottom),但是文本没有正确对齐。
请注意,我不喜欢使用形状,因为最终结果将是九块图像(来自 Photoshop)。尽管随意提供一个示例以供文档使用。
另请注意,它需要可点击。
我想我只需要为此创建一个自定义 XML,然后以某种方式让一个按钮使用它。我只是还没有弄清楚我需要如何/在哪里设置文本等等。
对此的任何建议表示赞赏。
Image comments: Icon should go centered in the red bit. The text should go centered in the yellow bit.
I'm trying to create a button in android that looks pretty much like the above. I tried with the imagebutton, the normal button (with drawabletop and drawablebottom), but the text is not proper aligned then.
Do note that I prefer not to use shapes, as the final result will be ninepatch images (that come from photoshop). Although feel free an example there for documentation use.
Also note that it needs to be clickable.
I think i just need to create a custom XML for this below, and then somehow have a button make use of this. I just haven't figured out how/where i need to set the text and so on then.
Any advice on this is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗨....
使用 Button 时...您可以使用
android:gravity="center"
android:text="TestHere"`根据您的期望对齐文本
尝试使用这个...在我的情况下,这有效 f9...它对您有帮助或没有帮助,请告诉我
hiii....
While using Button... you can align your text as per your expectations using
android:gravity="center"
android:text="TestHere"`Try using this... In my case this works f9... It does or doesn't help you plz let me know
您可以将下部创建为单独的按钮,将上部创建为图像视图。在布局文件中,将顶部部分放在底部部分上方。最后,在代码中创建一个 on clicklistener 并将相同的侦听器分配给按钮和图像。当然,这只会导致底部像按钮一样工作(看起来像是被按下的),但也许这是可以接受的。
You could create the lower part as a separate button, and the above part as an imageview. In your layout file, place the top part above the bottom part. And finally, in your code, create a on clicklistener and assign the same listener to both the button and the image. Of course this would only cause the bottom part to act like a button(looking like it's pressed), but perhaps that's acceptable.
当您想要使用九个补丁时,请创建九个补丁,使内容区域仅位于黄色部分的中间。我相信您知道如何为九个补丁选择内容区域。
As you want to use Nine patch, create nine patch such a way that the content area is only in the middle of the yellow portion. I'm sure you know how to select content area for nine patch.