栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我通过创建一个新的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
上的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);     } });

要改回它,你可以使用以下命令:

if (Build.VERSION.SDK_INT < 23) {    myTextView.setTextAppearance(getApplicationContext(), R.style.normalText);} else{   myTextView.setTextAppearance(R.style.normalText);}myTextView.setBackgroundResource(R.color.normalTextViewColor);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/420389.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号