如何在运行时更改 TextView 的样式

IT小君   2021-09-26T06:30:26

我有一个 android 应用程序,当用户点击 a 时TextView,我想应用定义的样式。

我想找到一个,textview.setStyle()但它不存在。我试过

textview.setTextAppearance();

但它不起作用。

点击广告,支持我们为你提供更好的服务
评论(8)
IT小君

我通过创建一个新的 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 来捕获该 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);
2021-09-26T06:30:27   回复
IT小君

就像乔纳森建议的那样,使用textView.setTextTypeface作品,我几秒钟前刚刚在一个应用程序中使用它。

textView.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.
2021-09-26T06:30:27   回复
IT小君
TextView tvCompany = (TextView)findViewById(R.layout.tvCompany);
tvCompany.setTypeface(null,Typeface.BOLD);

你可以从代码中设置它。字体

2021-09-26T06:30:27   回复
IT小君

以编程方式:运行时

您可以使用 setTypeface() 以编程方式完成:

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:设计时

您也可以在 XML 中设置:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

希望这会有所帮助

总和

2021-09-26T06:30:28   回复
IT小君

我发现textView.setTypeface(Typeface.DEFAULT_BOLD);这是最简单的方法。

2021-09-26T06:30:28   回复
IT小君

试试这行代码。

textview.setTypeface(textview.getTypeface(), Typeface.DEFAULT_BOLD);

在这里,它将从此文本视图中获取当前字体并使用新字体替换它。这里是新字体,DEFAULT_BOLD但您可以应用更多字体

2021-09-26T06:30:28   回复
IT小君

在 TextView http://developer.android.com/reference/android/widget/TextView.html 中查看 setText() 的 doco

要设置字符串样式,请将 android.text.style.* 对象附加到 SpannableString,或查看可用资源类型文档以获取在 XML 资源文件中设置格式化文本的示例。

2021-09-26T06:30:28   回复
IT小君

根据您要设置的样式,您必须使用不同的方法。TextAppearance 有自己的 setter,TypeFace 有自己的 setter,background 有自己的 setter,等等。

2021-09-26T06:30:29   回复