简体   繁体   English

创建具有自定义布局的按钮 android

[英]Create a button with custom layout android

图标应该在红色位居中。文本应该在黄色位居中。

Image comments: Icon should go centered in the red bit.图片评论:图标应该 go 以红色位居中。 The text should go centered in the yellow bit.文本应 go 以黄色位居中。

I'm trying to create a button in android that looks pretty much like the above.我正在尝试在 android 中创建一个看起来很像上面的按钮。 I tried with the imagebutton, the normal button (with drawabletop and drawablebottom), but the text is not proper aligned then.我尝试使用 imagebutton,普通按钮(带有 drawabletop 和 drawablebottom),但文本没有正确对齐。

Do note that I prefer not to use shapes, as the final result will be ninepatch images (that come from photoshop).请注意,我不喜欢使用形状,因为最终结果将是 Ninepatch 图像(来自 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.我想我只需要为此创建一个自定义 XML ,然后以某种方式使用一个按钮。 I just haven't figured out how/where i need to set the text and so on then.我只是还没弄清楚我需要如何/在哪里设置文本等等。

Any advice on this is appreciated.对此的任何建议表示赞赏。

You could create the lower part as a separate button, and the above part as an imageview.您可以将下部创建为单独的按钮,将上述部分创建为 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.最后,在您的代码中,创建一个 on clicklistener 并将相同的侦听器分配给按钮和图像。 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.我相信您知道如何在 select 内容区进行九个补丁。

hiii....喂……
While using Button... you can align your text as per your expectations using android:gravity="center" android:text="TestHere"`使用按钮时...您可以使用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尝试使用这个......在我的情况下,这适用于f9......它对你有帮助或没有帮助,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM