我想到找到一个textview.setStyle()但它不存在。我试过了
textview.setTextAppearance();
但它不工作。
<?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来捕获该TextView上的敲击事件:
编辑:
从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);