如何改变文本/字体设置在一个Android TextView?

例如,如何使文本加粗?


当前回答

通过XML:

 android:textStyle="bold"

通过Java:

//Let's say you have a textview 
textview.setTypeface(null, Typeface.BOLD);

其他回答

在Kotlin中,我们可以在一行中完成

 TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)

在我的情况下,通过string.xml通过html标签传递值..

<string name="your_string_tag"> <b> your_text </b></string> .

你可以这样做

ty.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/magistral.ttf"), Typeface.BOLD);

4种方法使Android TextView粗体-完整的答案在这里。

Using android:textStyle attribute <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEXTVIEW 1" android:textStyle="bold" /> Use bold|italic for bold and italic. using setTypeface() method textview2.setTypeface(null, Typeface.BOLD); textview2.setText("TEXTVIEW 2"); HtmlCompat.fromHtml() method, Html.fromHtml() was deprecated in API level 24. String html="This is <b>TEXTVIEW 3</b>"; textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));

要在layout.xml文件中这样做:

android:textStyle

例子:

android:textStyle="bold|italic"

程序上的方法是:

setTypeface(Typeface tf)

设置应显示文本的字体和样式。注意,并不是所有字体家族都有粗体和斜体变体,所以你可能需要使用setTypeface(Typeface, int)来获得你真正想要的外观。