在Android的TextView中如何将字体设置为粗体、斜体和带下划线?

530

我想让一个 TextView 的内容加粗、斜体和带下划线。我尝试了以下代码,它可以实现加粗和斜体,但无法实现下划线。

<Textview android:textStyle="bold|italic" ..

我该如何做?有什么快速的想法吗?


只设置其中一个是否有效? - falstro
是的,运作得很好。我也想把它加下划线。 - d-man
6
textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); 可以翻译为:textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); - bCliks
21
tv.setTypeface(null, Typeface.BOLD_ITALIC);这行代码的作用是将tv的字体设置为粗斜体。 - user2742371
下划线TextView - 5种惊人的方法 - Vijay Ram
12个回答

397

这应该可以同时使您的TextView 粗体下划线斜体

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"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/register" />

JAVA中,

TextView textView = new TextView(this);
textView.setText(R.string.register);
有时候,以上方法可能并不适用于你需要使用动态文本的情况。因此,在这种情况下,SpannableString 就会派上用场。
String tempString="Copyright";
TextView text=(TextView)findViewById(R.id.text);
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
text.setText(spanString);

输出

这里输入图片描述


3
我已经在2.1版本中检查过了,因此至少应该能够在2.1及以上版本中正常工作。 - Andro Selva
1
可以考虑使用 new StyleSpan(Typeface.BOLD_ITALIC) - Cheok Yan Cheng
为什么动态拼接的字符串不起作用?奇怪的是出现了一些数字.... - AuBee

308

52
给文本添加下划线:textView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); - bCliks
2
@bala请注意,您的解决方案始终会在整个文本下划线,因此在只想下划线其中一部分的情况下不可行。 - Giulio Piancastelli
如果可能的话,使用XML而不是编写代码更好。请参见Andro Selva的答案。 - El Sushiboi

178

或者在 Kotlin 中可以这样写:

val tv = findViewById(R.id.textViewOne) as TextView
tv.setTypeface(null, Typeface.BOLD_ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)
// OR
tv.setTypeface(null, Typeface.BOLD)
// OR
tv.setTypeface(null, Typeface.ITALIC)
// AND
tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG

或者在Java中:

TextView tv = (TextView)findViewById(R.id.textViewOne);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);
// OR
tv.setTypeface(null, Typeface.BOLD);
// OR
tv.setTypeface(null, Typeface.ITALIC);
// AND
tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);

保持简单,一行搞定 :)


2
通过在您正在使用的视图中插入kotlinx.android.synthetic包,Kotlin中不需要findViewByID,从而使每个setTypeface行:textViewOne.setTypeface(...)。 - cren90
paintFlags 是必要的吗?没有它也能工作。 - Prabs

86

对于粗体和斜体,您正在进行的操作是正确的。对于下划线,请使用以下代码:

HelloAndroid.java

 package com.example.helloandroid;

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.SpannableString;
 import android.text.style.UnderlineSpan;
import android.widget.TextView;

public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textview = (TextView)findViewById(R.id.textview);
    SpannableString content = new SpannableString(getText(R.string.hello));
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
    textview.setText(content);
}
}

主要.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
android:textStyle="bold|italic"/>

string.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string name="hello">Hello World, HelloAndroid!</string>
  <string name="app_name">Hello, Android</string>
</resources>

为了去掉下划线,可以将 Null 值传递给 setSpan 方法中的参数,而不是传递 new UnderlineSpan(),如下所示:content.setSpan(null, 0, content.length(), 0); - Sami Eltamawy

51

这是一种简单的方法来添加下划线,同时保持其他设置:

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

请注意,此解决方案始终会在整个文本下划线,因此在只想下划线其中一部分的情况下不可行。 - Giulio Piancastelli

48

可以通过setTypeface()方法进行编程实现:

以下是默认Typeface的代码:

textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

如果您想设置自定义字体:

textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      // for Normal Text
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);        // for Bold only
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      // for Italic
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic

XML:

你可以在XML文件中直接设置,例如:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

我该如何使用setTypeface更改字体系列并应用粗体、斜体和下划线? - Prince Dholakiya
@DPrince 看这里 https://dev59.com/-2ct5IYBdhLWcg3wZMfn - King of Masses

27

如果您从文件或网络中读取该文本。

您可以通过添加HTML标签来实现,就像所提到的那样。

This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>

接着您可以使用HTML类,将HTML字符串处理为可显示的样式文本。

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));

fromHtml(String) 方法在 API 级别 24 中已被弃用。 更多讨论请参见: https://dev59.com/_loU5IYBdhLWcg3wAjnV - Pavel Biryukov

21

没有引号对我有效:

<item name="android:textStyle">bold|italic</item>

14

通过使用 Kotlin 的 core-ktx 依赖项下的 buildSpannedString{},您可以轻松实现它。

val formattedString = buildSpannedString {
    append("Regular")
    bold { append("Bold") }
    italic { append("Italic") }
    underline { append("Underline") }
    bold { italic {append("Bold Italic")} }
}

textView.text = formattedString

13

只需一行xml代码

        android:textStyle="italic"

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