Android:结合文本和按钮或 ImageButton 上的图像 [英] Android: combining text & image on a Button or ImageButton
问题描述
我正在尝试在按钮上放置一个图像(作为背景),并根据运行时发生的情况动态添加图像上方/上方的一些文本.
I'm trying to have an image (as the background) on a button and add dynamically, depending on what's happening during run-time, some text above/over the image.
如果我使用 ImageButton
我什至无法添加文本.如果我使用 Button
,我可以添加文本,但只能使用 android:drawableBottom
和定义的类似 XML 属性定义图像 此处.
If I use ImageButton
I don't even have the possibility to add text.
If I use Button
I can add text but only define an image with android:drawableBottom
and similar XML attributes as defined here.
然而,这些属性只结合了文本 &x 和 y 维度的图像,这意味着我可以围绕我的文本绘制图像,但不能在我的文本下方/下方(z 轴定义为从显示中出来).
However these attributes only combine text & image in x- and y-dimensions, meaning I can draw an image around my text, but not below/under my text (with the z-axis defined as coming out of the display).
关于如何做到这一点的任何建议?一种想法是扩展 Button
或 ImageButton
并覆盖 draw()
方法.但以我目前的知识水平,我真的不知道如何做到这一点(2D 渲染).也许有更多经验的人知道解决方案或至少有一些开始的指示?
Any suggestions on how to do this? One idea would be to either extend Button
or ImageButton
and override the draw()
-method. But with my current level of knowledge I don't really know how to do this (2D rendering). Maybe someone with more experience knows a solution or at least some pointers to start?
推荐答案
您可以在 Button
上调用 setBackground()
来设置按钮的背景.
You can call setBackground()
on a Button
to set the background of the button.
任何文本都会出现在背景之上.
Any text will appear above the background.
如果您正在 xml 中寻找类似的东西,则有:android:background
属性,其工作方式相同.
If you are looking for something similar in xml there is:
android:background
attribute which works the same way.
这篇关于Android:结合文本和按钮或 ImageButton 上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!