Java 如何更改文本视图';运行时的s样式
我有一个android应用程序,当用户点击一个Java 如何更改文本视图';运行时的s样式,java,android,textview,Java,Android,Textview,我有一个android应用程序,当用户点击一个TextView时,我想应用一个定义的样式 我想找到一个textview.setStyle(),但它不存在。我试过了 textview.setTextAppearance(); 但它不起作用。请参阅TextView中的setText()文档 要设置字符串的样式,请将android.text.style.*对象附加到SpannableString,或参阅可用的资源类型文档以获取在XML资源文件中设置格式化文本的示例 为此,我创建了一个新的XML文件
TextView
时,我想应用一个定义的样式
我想找到一个textview.setStyle()
,但它不存在。我试过了
textview.setTextAppearance();
但它不起作用。请参阅TextView中的setText()文档
要设置字符串的样式,请将android.text.style.*对象附加到SpannableString,或参阅可用的资源类型文档以获取在XML资源文件中设置格式化文本的示例
为此,我创建了一个新的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>
粗体|斜体
#FFFFFF
正常的
#C0C0C0
我的“strings.xml”文件中还有如下条目:
<color name="highlightedTextViewColor">#000088</color>
<color name="normalTextViewColor">#000044</color>
#000088
#000044
然后,在我的代码中,我创建了一个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);
}
});
myTextView.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
//突出显示文本视图
//myTextView.setTextAppearance(getApplicationContext(),R.style.boldText);
如果(Build.VERSION.SDK_INT<23){
myTextView.setTextAppearance(getApplicationContext(),R.style.boldText);
}否则{
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);
if(Build.VERSION.SDK\u INT<23){
myTextView.setTextAppearance(getApplicationContext(),R.style.normalText);
}否则{
myTextView.setTextAppearance(R.style.normalText);
}
myTextView.setBackgroundResource(R.color.normalTextViewColor);
就像Jonathan建议的那样,使用textView.setTextTypeface
可以工作,几秒钟前我刚刚在一个应用程序中使用了它
textView.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.
您可以从代码中设置它 我找到了
textView.setTypeface(Typeface.DEFAULT\u BOLD)
是最简单的方法。根据要设置的样式,必须使用不同的方法。text外观有自己的setter,字体有自己的setter,背景有自己的setter,等等。试试这行代码
textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);
在这里,它将从此文本视图中获取当前字体,并使用新字体替换它。这里的新字体是
DEFAULT\u BOLD
,但您可以应用更多字体 以编程方式:运行时
您可以使用setTypeface()以编程方式执行以下操作:
XML:设计时
您还可以在XML中设置:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
希望这会有所帮助
Summaved我在某个地方读到,这是不可能的。但是您可以单独设置一些属性。您是否尝试过:textView.setTextTypeface@Falmari你看过TextView api文档了吗?如果是这样,也许你可以说一些更有建设性的话。你可以使用textView.setTypeface(null,Typeface.BOLD);textView..setTypeface(null,Typeface.ITALIC);textView.setTypeface(null,Typeface.BOLD_ITALIC);setTypeface(null,Typeface.BOLD)对我来说很有用。当然,您可以使用1来代替,但我想手动输入常量不是一个好的做法。最好在库中使用预定义变量。不要鼓励人们使用int而不是constantsGood!我的错误是在第一个参数中发送我的旧字体。因此,当我更改NORMAL->BOLD但不更改BOLD->NORMAL时,它确实起作用。我不知道第一个参数可以为null!现在对我来说很好!实际上,这不适用于阴影样式。如果你想改变阴影,你必须这样做:lockText.setShadowLayer(20,4,4,0xFFFF0000);为了避免SDK检查,可以使用TextViewCompat.setTextAppearance(myTextView,R.style.myStyle);你最好解释一下为什么你的代码能解决他的问题,谢谢。OP想要改变的是样式,而不是字体。这会改变字体。虽然不是OP.True指定的样式,但这仍然是最简单的方法——就我所记得的唯一的方法——在撰写本文时。我不确定是否同时添加了一个新的API方法,它可以在不改变字体的情况下将“样式”更改为粗体。如果我错了,请纠正我。
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
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"