Android安卓TextView部分字体颜色

举报
第三女神程忆难 发表于 2021/03/26 01:27:04 2021/03/26
7.3k+ 0 0
【摘要】 TextView修改部分文字颜色 Java代码或Kotlin代码布局文件其运行效果 Java代码 String colorText = "这里是<font color = \"#FF0000\">红色</font>的,这里是<font color = \"#03A9F4\">蓝色</font>的"; TextView textView = findV...

TextView修改部分文字颜色


Java代码

String colorText = "这里是<font color = \"#FF0000\">红色</font>的,这里是<font color = \"#03A9F4\">蓝色</font>的";
TextView textView = findViewById(R.id.textView);
textView.setText(Html.fromHtml(colorText));

  
 

或Kotlin代码

 //修改TextView部分文字颜色
val colorText = "这里是<font color = \"#FF0000\">红色</font>的,这里是<font color = \"#03A9F4\">蓝色</font>的"
val textView = findViewById<TextView>(R.id.textView)
textView.text = Html.fromHtml(colorText)

  
 

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:textSize="18dp" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

  
 

其运行效果

在这里插入图片描述

文章来源: blog.csdn.net,作者:第三女神程忆难,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_40881680/article/details/99290158

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。