如何在 ImageButton 上显示文本?
我有一个 ImageButton,我想在上面显示文本和图像。但是当我尝试使用模拟器时:
<ImageButton
android:text="OK"
android:id="@+id/buttonok"
android:src="@drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
我得到了图像,但没有文本。我怎样才能显示文字?请帮我!
I have an ImageButton
and I want to show a text and an image on it. But when I try on emulator:
<ImageButton
android:text="OK"
android:id="@+id/buttonok"
android:src="@drawable/buttonok"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I get the image but without the text. How can I show the text? Please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我通过将
ImageButton
和TextView
放入垂直方向的LinearLayout
中解决了这个问题。效果很好!I solved this by putting the
ImageButton
andTextView
inside aLinearLayout
with vertical orientation. Works great!您可以使用
LinearLayout
而不是使用Button
这是我在我的应用程序中使用的安排You can use a
LinearLayout
instead of usingButton
it's an arrangement i used in my app您可以使用常规
Button
和 android:drawableTop 属性(或左、右、下)。you can use a regular
Button
and the android:drawableTop attribute (or left, right, bottom) instead.最好的办法:
Best way:
这是一个很好的圆形示例:
drawable/circle.xml
:然后是 xml 文件中的按钮:
Heres a nice circle example:
drawable/circle.xml
:And then the button in your xml file:
这是解决方案
Here is the solution
在
按钮
上显示文本
的最佳方式(带图像)您的问题:如何显示
图像按钮
上的文本
?回答:您无法使用
imageButton
显示text
。 接受的答案中告诉的方法也不起作用。因为
如果你使用
android:drawableLeft="@drawable/buttonok"
那么你不能在button
center 设置drawable
代码>.如果您使用
android:background="@drawable/button_bg"
,那么drawable
的color
将发生变化。在 Android 世界中,有数以千计的选项可以做到这一点。但在这里我根据我的观点提供最佳替代方案。 (见下文)
您的
drawable/image
在LinearLayout
中使用,因为它显示在中心。在textView
的帮助下,您可以对此设置text
。我们将cardView
背景设置为透明
。在这里我解释一些术语:
app:cardBackgroundColor="@android:color/transparent"
用于使cardView
的背景透明app:cardElevation="0dp"
code> 用于隐藏cardView
周围的水平线app:cardUseCompatPadding="true"
它提供cardView
的实际大小。当您使用cardView
将
LinearLayout
中的image/drawable
设置为背景时,请始终使用此选项。抱歉,我的英语不好。
快乐编码:)
Best way to show
Text
onbutton
(with image)Your Question: How to show
text
onimagebutton
?Answer: You can not display
text
withimageButton
. Method that tell in Accepted answer also not work.because
If you use
android:drawableLeft="@drawable/buttonok"
then you can not setdrawable
incenter
ofbutton
.If you use
android:background="@drawable/button_bg"
thencolor
of yourdrawable
will be changed.In android world there are thousands of option to do this. But here i provide best alternate according to my point of view. (see below)
Your
drawable/image
use inLinearLayout
because it shows in center. And with help oftextView
you can settext
on this. We makescardView
background totransparent
.here i explain some terms:
app:cardBackgroundColor="@android:color/transparent"
for make transparent background ofcardView
app:cardElevation="0dp"
for hide evelation lines aroundcardView
app:cardUseCompatPadding="true"
its provide actual size ofcardView
. Always use this when you usecardView
set your
image/drawable
inLinearLayout
as a background.Sorry, for my Bad English.
Happy Coding:)
ImageButton
不能有text
(或者,至少android:text
未在其属性中列出)。技巧是:
看起来您需要使用
Button
(并查看drawableTop
或setCompoundDrawablesWithIntrinsicBounds(int,int,int,int))
。ImageButton
can't havetext
(or, at least,android:text
isn't listed in its attributes).The Trick is:
It looks like you need to use
Button
(and look atdrawableTop
orsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int))
.由于您无法使用
android:text
我建议您使用普通按钮并使用复合绘图之一。例如:您可以使用以下方式将可绘制对象放置在任何位置:
drawableTop
、drawableBottom
、drawableLeft
或drawableRight
。更新
对于按钮来说,这也工作得很好。放置
android:background
就可以了!我刚刚遇到这个问题并且工作正常。
As you can't use
android:text
I recommend you to use a normal button and use one of the compound drawables. For instance:You can put the drawable wherever you want by using:
drawableTop
,drawableBottom
,drawableLeft
ordrawableRight
.UPDATE
For a button this too works pretty fine. Putting
android:background
is fine!I just had this issue and is working perfectly.
如果您确实想要的话,在技术上可以在
ImageButton
上添加标题。只需使用FrameLayout
将TextView
放在ImageButton
上即可。请记住不要使Textview
可点击。例子:
It is technically possible to put a caption on an
ImageButton
if you really want to do it. Just put aTextView
over theImageButton
usingFrameLayout
. Just remember to not make theTextview
clickable.Example:
伙计们,我需要开发设置和注销按钮,我使用了下面的代码。

Guys I need to develop the setting and logout button, I used the below code.

实际上,
android:text
不是ImageButton
接受的参数但是,如果您尝试获取具有指定背景(不是 android 默认)的按钮,请使用
android:background
xml 属性,或使用.setBackground(); 从类中声明它;
Actually,
android:text
is not an argument accepted byImageButton
but, If you're trying to get a button with a specified background (not android default) use the
android:background
xml attribute, or declare it from the class with.setBackground();