如何在 Android 中为按钮文本添加下划线?
How to Underline Text of Button in Android?
我有一个透明的按钮,上面有图标和文字。
我想给按钮的文本加下划线,但我做不到。
下面是我的 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。
有什么建议吗?
仅代码
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
中使用以下方法:
<string name="underlined_text"><u>I\'m underlined</u></string>
带有动态文本的资源字符串(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")
使用这个:
TextView txt=(TextView)findViewById(R.id.txt);
String styledText = "<u>parking areas</u>";
txt.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
这应该使您的 ButtonText 粗体、下划线 和 斜体同时.
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" />
Button button= (Button) findViewById(R.id.park);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
button.setText(content);
您不能在 xml 文件中设置下划线。要使用代码设置下划线,您需要在按钮上设置下划线标志。
Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
我有一个透明的按钮,上面有图标和文字。
我想给按钮的文本加下划线,但我做不到。
下面是我的 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。 有什么建议吗?
仅代码
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
中使用以下方法:
<string name="underlined_text"><u>I\'m underlined</u></string>
带有动态文本的资源字符串(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")
使用这个:
TextView txt=(TextView)findViewById(R.id.txt);
String styledText = "<u>parking areas</u>";
txt.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
这应该使您的 ButtonText 粗体、下划线 和 斜体同时.
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" />
Button button= (Button) findViewById(R.id.park);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
button.setText(content);
您不能在 xml 文件中设置下划线。要使用代码设置下划线,您需要在按钮上设置下划线标志。
Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);