如何在Android中将文本加粗?

515

如何更改 Android TextView 中的文本/字体设置?

例如,如何使文本加粗?


请查看我在此处的答案:(http://stackoverflow.com/a/40802895/1252158)。 希望这能帮到你。 - Summved Jain
3
请点击此处阅读有关如何在Android TextView中设置粗体字的文章。 - Thomas Daniel
22个回答

645

layout.xml 文件中实现此操作:

android:textStyle

示例:

android:textStyle="bold|italic"

这个方法的程序化实现方式是:

setTypeface(Typeface tf)

设置文本应该显示的字体和样式。请注意,并不是所有的Typeface字体系列都有粗体和斜体变体,因此您可能需要使用setTypeface(Typeface,int)来获得您实际想要的外观。


380

这里是解决方案

TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);

如果编辑文本视图组件,能否以不同的方式加粗每一行? - gumuruh
是的,这是可能的。 - Sudipta Som

89

只需要按照以下方式操作:

XML 中设置属性即可。

  android:textStyle="bold"

编程方法如下:

TextView Tv = (TextView) findViewById(R.id.TextView);

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);

Tv.setTypeface(boldTypeface);

希望这能对你有所帮助,谢谢。


请告诉我结果。 - saeed

55

在XML中

android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic

您只能通过 xml 使用特定字体sansserifmonospaceJava 代码可以使用自定义字体。

android:typeface="monospace" // or sans or serif

编程方式(Java代码)

TextView textView = (TextView) findViewById(R.id.TextView1);

textView.setTypeface(Typeface.SANS_SERIF); //only font style
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD); 
                                         //font style & text style(only bold)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
                                         //font style & text style(bold & italic)

28

如果您正在使用自定义字体,但是没有该字体的粗体字形,您可以使用以下方法:

myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");

28

从XML中,您可以将textStyle设置为如下的bold

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Bold text"
   android:textStyle="bold"/>

您可以通过以下方式以编程方式将 TextView 设置为加粗

textview.setTypeface(Typeface.DEFAULT_BOLD);

22

设置属性

android:textStyle="bold"

22

非常容易

setTypeface(Typeface.DEFAULT_BOLD);

19

通过 XML:

 android:textStyle="bold"

通过Java实现:
//Let's say you have a textview 
textview.setTypeface(null, Typeface.BOLD);

17

如果你要绘制它,那么这就是所需的代码:

TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);

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