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));
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
现在设置textview属性…
text.setTypeface(null, Typeface.BOLD); //-- for only bold the texttext.setTypeface(null, Typeface.BOLD_ITALIC); //-- for bold & italic the texttext.setTypeface(null, Typeface.ITALIC); // -- for italic the text
textView.setTypeface(null, Typeface.NORMAL); // for Normal TexttextView.setTypeface(null, Typeface.BOLD); // for Bold onlytextView.setTypeface(null, Typeface.ITALIC); // for ItalictextView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
textView.setTypeface(null, Typeface.NORMAL); // for Normal TexttextView.setTypeface(null, Typeface.BOLD); // for Bold onlytextView.setTypeface(null, Typeface.ITALIC); // for ItalictextView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic
如果你想设置自定义字体:
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL); // for Normal TexttextView.setTypeface(textView.getTypeface(), Typeface.BOLD); // for Bold onlytextView.setTypeface(textView.getTypeface(), Typeface.ITALIC); // for ItalictextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic
String pre = "", post = "";
if(isBold){pre += "<b>"; post += "</b>";}if(isItalic){pre += "<i>"; post += "</i>";}if(isUnderline){pre += "<u>"; post += "</u>";}
textView.setText(Html.fromHtml(pre + editText.getText().toString()+ post));// you can also use it with EidtTexteditText.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
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));