如何改变文本/字体设置在一个Android TextView?
例如,如何使文本加粗?
如何改变文本/字体设置在一个Android TextView?
例如,如何使文本加粗?
当前回答
在我的情况下,通过string.xml通过html标签传递值..
<string name="your_string_tag"> <b> your_text </b></string> .
其他回答
在我的情况下,通过string.xml通过html标签传递值..
<string name="your_string_tag"> <b> your_text </b></string> .
在values文件夹中的style.xml文件中定义一个新的样式,使用您想要的格式
<style name="TextViewStyle" parent="AppBaseTheme">
<item name="android:textStyle">bold</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#5EADED</item>
</style>
然后通过编写以下带有TextView属性的代码将此样式应用到TextView
style="@style/TextViewStyle"
textView.setPaintFlags(textView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG)
要移除,请使用
textView.setPaintFlags(textView.getPaintFlags() & ~Paint.FAKE_BOLD_TEXT_FLAG)
或者用Kotlin:
fun TextView.makeBold() {
this.paintFlags = this.paintFlags or Paint.FAKE_BOLD_TEXT_FLAG
}
fun TextView.removeBold() {
this.paintFlags = this.paintFlags and (Paint.FAKE_BOLD_TEXT_FLAG.inv())
}
在使用自定义字体,但没有粗体字体的情况下,可以使用:
myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");
你可以简单地做以下几点:
以XML格式设置属性
android:textStyle="bold"
从程序上看,方法是:
TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);
希望这能帮到你,谢谢。