如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
当前回答
编程:
你可以通过编程方式使用setTypeface():
textView.setTypeface(null, Typeface.NORMAL); // for Normal Text
textView.setTypeface(null, Typeface.BOLD); // for Bold only
textView.setTypeface(null, Typeface.ITALIC); // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
XML:
你可以在<TextView />中的XML文件中直接设置如下:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
其他回答
很简单,如果你想让文本加粗。 将这一行写在布局中的文本视图属性中
android:textStyle="bold"
你可以这样尝试:
<string name="title"><u><b><i>Your Text</i></b></u></string>
你有两个选择:
选项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:
使用可伸缩帆布;它更复杂,但您可以动态修改文本属性(不仅是粗体/斜体,还包括颜色)。
在我的例子中:
1 -设置文本
2 -设置字体
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
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)