Cri*_*ris 97 java android textview
我有一个Android应用程序,当用户点击一个TextView,我想应用一个定义的样式.
我想找到一个,textview.setStyle()但它不存在.我试过了
textview.setTextAppearance();
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
Gle*_*enn 142
我这样做是通过创建一个新的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>
Run Code Online (Sandbox Code Playgroud)
我的"strings.xml"文件中也有一个条目,如下所示:
<color name="highlightedTextViewColor">#000088</color>
<color name="normalTextViewColor">#000044</color>
Run Code Online (Sandbox Code Playgroud)
然后,在我的代码中,我创建了一个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);
}
});
Run Code Online (Sandbox Code Playgroud)
要将其更改回来,您可以使用:
if (Build.VERSION.SDK_INT < 23) {
myTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
} else{
myTextView.setTextAppearance(R.style.normalText);
}
myTextView.setBackgroundResource(R.color.normalTextViewColor);
Run Code Online (Sandbox Code Playgroud)
DKD*_*ude 90
就像Jonathan建议的那样,使用textView.setTextTypeface作品,我几秒钟之前就在应用程序中使用过它.
textView.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.
Run Code Online (Sandbox Code Playgroud)
Azh*_*har 12
TextView tvCompany = (TextView)findViewById(R.layout.tvCompany);
tvCompany.setTypeface(null,Typeface.BOLD);
Run Code Online (Sandbox Code Playgroud)
你可以从代码中设置它.字体
| 归档时间: |
|
| 查看次数: |
100463 次 |
| 最近记录: |