Java 如何设置图像按钮的文本(Android)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3609026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set text of Image Button (Android)?
提问by Rohan K
In my Android project, I've a image button whose background is set to some image. I want so set some text on the image button. How do i do that/
在我的 Android 项目中,我有一个图像按钮,其背景设置为某个图像。我想在图像按钮上设置一些文字。我怎么做/
I want to set the text because localization would be used in the project....
我想设置文本,因为项目中将使用本地化....
采纳答案by Maragues
An ImageButton is not supposed to have text, that's why it's called ImageButton.
ImageButton 不应该有文本,这就是它被称为 ImageButton 的原因。
Try to use Button, which inherits from TextView and does have the android:text attribute.
尝试使用Button,它继承自 TextView 并且确实具有 android:text 属性。
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mail"
android:background="@drawable/btn_selector"/>
Is there any particular reason why you want to use ImageButton over Button?
您想在 Button 上使用 ImageButton 有什么特别的原因吗?
回答by Steven
Rather than using the ImageButton, just use a regular button and use the drawable attributes. There are several depending on where you want the drawable to be.
而不是使用 ImageButton,只需使用常规按钮并使用 drawable 属性。有几个取决于您希望 drawable 的位置。
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableBottom
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableBottom
回答by Sami Eltamawy
I was having the same problem. I have solved this by adding a TextView under the ImageButton and set its Text. If you used Button instead and tried to set your picture as a background, you will find out the Text in the middle of the picture and will not be obvious. But, in this way you will have the shape of Windows Icon above the file name.
我遇到了同样的问题。我通过在 ImageButton 下添加一个 TextView 并设置它的 Text 来解决这个问题。如果你改用Button,并试图将你的图片设置为背景,你会发现图片中间的文本不会很明显。但是,通过这种方式,您将在文件名上方看到 Windows 图标的形状。