android - 如何在 ImageButton 上显示文本?

标签 android imagebutton

我有一个 ImageButton,我想在上面显示文本和图像。但是当我尝试模拟器时:

<ImageButton 
    android:text="OK" 
    android:id="@+id/buttonok" 
    android:src="@drawable/buttonok"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" />

我得到了图像,但没有文字。如何显示文本?请帮帮我!

最佳答案

由于您不能使用 android:text,我建议您使用普通按钮并使用复合可绘制对象之一。例如:

<Button 
    android:id="@+id/buttonok" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/buttonok"
    android:text="OK"/>

您可以使用 drawableTopdrawableBottomdrawableLeftdrawableRight 将可绘制对象放在任何您想要的位置。

更新

对于一个按钮,这也很好用。放 android:background 就可以了!

<Button
    android:id="@+id/fragment_left_menu_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_bg"
    android:text="@string/login_string" />

我刚遇到这个问题,并且工作正常。

关于android - 如何在 ImageButton 上显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4457030/

相关文章:

android - 如何预览requireSmallestWidthDp配置与哪些设备匹配?

android - 自定义编辑带边框的文本

android - RemoteViews setInt setImageResource 在 ImageButton 中不起作用

asp.net - ASP :ImageButton not firing onclick event

javascript - 使用 Javascript 在单击时隐藏 ImageButton

Android 按钮从布局返回 Activity

Android On Focus Listener 和 On ImageView 上的 On Click Listener

android - adb 安装命令不起作用

java - 单击时如何更改图像按钮背景?

Android ImageButton图片对比