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

例如,如何使文本加粗?


当前回答

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

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

其他回答

从XML中,您可以将textStyle设置为粗体,如下所示

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

您可以像下面这样通过编程方式将TextView设置为粗体

textview.setTypeface(Typeface.DEFAULT_BOLD);
editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);

将字体和样式都设置为粗体。

通过XML:

 android:textStyle="bold"

通过Java:

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

最好的方法是:

TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);

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

android:textStyle

例子:

android:textStyle="bold|italic"

程序上的方法是:

setTypeface(Typeface tf)

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