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

例如,如何使文本加粗?


当前回答

你可以简单地做以下几点:

以XML格式设置属性

  android:textStyle="bold"

从程序上看,方法是:

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

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

Tv.setTypeface(boldTypeface);

希望这能帮到你,谢谢。

其他回答

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

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

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

android:textStyle

例子:

android:textStyle="bold|italic"

程序上的方法是:

setTypeface(Typeface tf)

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

在.xml文件中,设置

android:textStyle="bold" 

将文本类型设置为粗体。

在使用自定义字体,但没有粗体字体的情况下,可以使用:

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

你可以这样做

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