Я сделал это, создав новый XML-файл res/values/style.xml
следующим образом:
<?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>
Тогда , в моем коде я создал ClickListener, чтобы захватить событие tap на этом TextView: EDIT:От A PI 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);
Вы смотрели через TextView API документы? – Falmarri
Я где-то читал, что это невозможно ... http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view. Но вы можете установить некоторые свойства отдельно. – Floern
Вы попробовали: textView.setTextTypeface? http://developer.android.com/reference/android/widget/TextView.html#setTypeface(android.graphics.Typeface) –