如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
当前回答
//leveraging the extension functions
fun TextView.makeBold(){
this.setTypeface(this.typeface,Typeface.BOLD)
}
yourTextView.makeBold()
其他回答
这是我在配置了OnePlus Slate™字体的OnePlus 5T上唯一有效的功能:
textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
其他方法将使它在BOLD或NORMAL时回落到Roboto。
你可以这样尝试:
<string name="title"><u><b><i>Your Text</i></b></u></string>
1)你可以用字体设置。 2)你可以直接在strings.xml中使用(在你的values文件夹中) 3)你可以String myNewString = " This is my bold text This is my italics String This is my underscore String .
textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);
textView.setTypeface(null, Typeface.NORMAL);
保持以前的字体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)
你有两个选择:
选项1(只适用于粗体、斜体和下划线):
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(Html.fromHtml(s));
选项2:
使用可伸缩帆布;它更复杂,但您可以动态修改文本属性(不仅是粗体/斜体,还包括颜色)。