如何在Android中给按钮的文本添加下划线?

41

我有一个透明的按钮,上面有图标和文本。

我想要给按钮的文本加下划线,但是一直没能成功。

以下是我的XML代码:

<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。 有什么建议吗?


1
请看这个链接:https://dev59.com/1Gsz5IYBdhLWcg3wOlSh#8033336 - Nivedh
是的,我已经阅读了这篇文章。这适用于TextView,这种情况下它可以完美地工作。但是同样的方法对于按钮文本不起作用。 - user3303274
6个回答

86

仅限代码

Java:

Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

Kotlin:

val button = findViewById<Button>(R.id.park);
button.paintFlags = button.paintFlags or Paint.UNDERLINE_TEXT_FLAG

使用静态文本的资源字符串(仅限xml)

如果您的资源中有静态文本,您也可以在strings.xml 中使用以下方法:

<string name="underlined_text"><u>I\'m underlined</u></string>

包含动态文本的资源字符串(xml + 代码)

如果您使用了动态文本但不喜欢第一种方法(在我看来也不是最好的),那么您可以尝试使用以下方法:

strings.xml

<string name="underlined_dynamic_text"><u>%s</u></string>

Java:

Java:
button.setText(getString(R.string.underlined_dynamic_text, "I'm underlined");

Kotlin:

button.text = getString(R.string.underlined_dynamic_text, "I'm underlined")

2
我花了很长时间才回来!但是现在通过对Android的更多经验,我意识到这个解决方案完全可以正常工作。感谢您的帮助。 - user3303274

16

这应该能够让你的按钮文本同时变成加粗下划线以及斜体

strings.xml

<resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>

要将此字符串设置到您的TextView中,请在您的main.xml中执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/register" />

我看不出你的代码和我的有什么区别。这是直接从另一篇Stackflow文章的答案复制的,但并没有起作用。 - user3303274
这段代码同样适用于按钮。你使用的是哪个操作系统版本? - Mohammad Arman
如何设置另一个资源字符串,例如@string/something,而不是版权文字? - ukod

6

您无法从XML文件中设置下划线。要使用代码设置下划线,您需要在按钮上设置下划线标志。


Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

我尝试了你的代码,但在Paint.UNDERLINE_TEXT_FLAG处出现了错误 - 有什么想法? - user3303274
尝试添加import android.graphics.Paint。如果仍然出现错误,请分享错误详细信息。 - Amit Padekar

3

使用以下代码:

TextView txt=(TextView)findViewById(R.id.txt);
        String styledText = "<u>parking areas</u>";
        txt.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

3
Button button= (Button) findViewById(R.id.park);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
button.setText(content);

0

使用Compose,您可以在Text中使用textDecoration属性:

Button(
    onClick = {}
) {
    Icon(
        Icons.Filled.Add,
        contentDescription = "Add",
    )
    Text(
        text = "Button",
        textDecoration = TextDecoration.Underline
    )
}

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接