如何更改 Android TextView
中的文本/字体设置?
例如,如何使文本加粗?
在 layout.xml
文件中实现此操作:
android:textStyle
示例:
android:textStyle="bold|italic"
这个方法的程序化实现方式是:
setTypeface(Typeface tf)
设置文本应该显示的字体和样式。请注意,并不是所有的Typeface
字体系列都有粗体和斜体变体,因此您可能需要使用setTypeface(Typeface,int)
来获得您实际想要的外观。
这里是解决方案
TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);
只需要按照以下方式操作:
在 XML
中设置属性即可。
android:textStyle="bold"
编程方法如下:
TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);
希望这能对你有所帮助,谢谢。
在XML中
android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic
您只能通过 xml 使用特定字体sans
、serif
和monospace
,Java 代码可以使用自定义字体。
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)
如果您正在使用自定义字体,但是没有该字体的粗体字形,您可以使用以下方法:
myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");
从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);
设置属性
android:textStyle="bold"
非常容易
setTypeface(Typeface.DEFAULT_BOLD);
通过 XML:
android:textStyle="bold"
//Let's say you have a textview
textview.setTypeface(null, Typeface.BOLD);
如果你要绘制它,那么这就是所需的代码:
TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);