3 回答

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個贊
我通過創(chuàng)建一個新的XML文件res/values/style.xml來做到這一點(diǎn),如下所示:
<?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>
我的“ strings.xml”文件中也有一個條目,如下所示:
<color name="highlightedTextViewColor">#000088</color>
<color name="normalTextViewColor">#000044</color>
然后,在我的代碼中,我創(chuàng)建了一個ClickListener來捕獲該TextView上的tap事件: 編輯: 自API 23起,不建議使用setTextAppearance
myTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
//highlight the TextView
//myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
if (Build.VERSION.SDK_INT < 23) {
myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
} else {
myTextView.setTextAppearance(R.style.boldText);
}
myTextView.setBackgroundResource(R.color.highlightedTextViewColor);
}
});
要將其改回,可以使用以下方法:
if (Build.VERSION.SDK_INT < 23) {
myTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
} else{
myTextView.setTextAppearance(R.style.normalText);
}
myTextView.setBackgroundResource(R.color.normalTextViewColor);

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個贊
就像喬納森(Jonathan)建議的那樣,在使用textView.setTextTypeface作品的同時,我只是在幾秒鐘前在一個應(yīng)用程序中使用過它。
textView.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個贊
以編程方式:運(yùn)行時
您可以使用setTypeface()以編程方式進(jìn)行操作:
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:設(shè)計(jì)時間
您還可以設(shè)置XML:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
希望這會有所幫助
總結(jié)
添加回答
舉報(bào)