如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
当前回答
在AndroidX中使用简化标签时,请考虑使用HtmlCompat.fromHtml()
String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"
TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
tv.setText(HtmlCompat.fromHtml(s, FROM_HTML_MODE_LEGACY));
其他回答
//leveraging the extension functions
fun TextView.makeBold(){
this.setTypeface(this.typeface,Typeface.BOLD)
}
yourTextView.makeBold()
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);
使用上述方法以编程方式设置字体。
它会是
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
和斜体应能与替换字体。DEFAULT_BOLD与字体。
让我知道它是如何工作的。
在我的例子中:
1 -设置文本
2 -设置字体
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
使用textView。setTypeface(字体tf, int风格);设置TextView的样式属性。有关更多信息,请参阅开发人员文档。