如何在Android中为Button文本加下划线? [英] How to Underline Text of Button in Android?
问题描述
我有一个透明的按钮,带有图标和文本.
I have a button which is transparent and has an icon and text.
我想在按钮的文本下划线,但我无法执行此操作.
I want to underline the text of the button but i have not been able to do this.
下面是我的xml代码:
Below is my xml code:
<Button
android:id="@+id/parkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/park"
android:text="@string/button_name"
android:background="@android:color/transparent"
android:textColor="@android:color/black"/>
字符串文件具有:
<resources>
<string name="button_name"><u>Parking Areas</u></string>
</resources>
此方法适用于TextView,但不适用于Button. 有什么建议吗?
This approach works in TextView, but not in Button. Any suggestion?
推荐答案
仅代码
Java:
Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
科特琳:
val button = findViewById<Button>(R.id.park);
button.paintFlags = button.paintFlags or Paint.UNDERLINE_TEXT_FLAG
带有静态文本的资源字符串(仅xml)
如果您的资源中包含静态文本,则还可以在strings.xml
中使用以下方法:
If you have a static text in your resources you could also use the following approach in your strings.xml
:
<string name="underlined_text"><u>I\'m underlined</u></string>
带有动态文本(XML +代码)的资源字符串
如果您使用动态文本,但不喜欢第一种方法(这也不是最好的恕我直言),则还可以使用以下方法:
If you're using dynamic text but don't like the first approach (which isn't the best imho either), you could also use following:
strings.xml
strings.xml
<string name="underlined_dynamic_text"><u>%s</u></string>
Java:
button.setText(getString(R.string.underlined_dynamic_text, "I'm underlined");
科特琳:
button.text = getString(R.string.underlined_dynamic_text, "I'm underlined")
这篇关于如何在Android中为Button文本加下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!