如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?

换句话说,我需要用Java编写android:textStyle。


当前回答

尝试通过java代码设置你的TextView样式

txt1.setTypeface(null,Typeface.BOLD_ITALIC);

其他回答

试试这个:

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);

现在设置textview属性..

text.setTypeface(null, Typeface.BOLD);  //-- for only bold the text
text.setTypeface(null, Typeface.BOLD_ITALIC);  //-- for  bold & italic the text
text.setTypeface(null, Typeface.ITALIC);  // -- for  italic the text
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 -设置文本

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)