是否有一种方法来设置TextView的textStyle属性编程?似乎没有setTextStyle()方法。
需要明确的是,我不是在谈论视图/小部件样式!我说的是以下几点:
<TextView
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textStyle="bold" />
实现这一任务的很多方法如下:-
1.
String text_view_str = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!";
TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
tv.setText(Html.fromHtml(text_view_str));
2.
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
3.
SpannableString spannablecontent=new SpannableString(o.content.toString());
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
0,spannablecontent.length(), 0);
// set Text here
tt.setText(spannablecontent);
4.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="boldText">
<item name="android:textStyle">bold|italic</item>
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="normalText">
<item name="android:textStyle">normal</item>
<item name="android:textColor">#C0C0C0</item>
</style>
</resources>
tv.setTextAppearance(getApplicationContext(), R.style.boldText);
或者通过XML
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"