为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 HTML 标记为 strings.xml 文件中的字符串着色?

如何使用 HTML 标记为 strings.xml 文件中的字符串着色?

江户川乱折腾 2023-02-16 16:28:44
我正在创建一个类似于 PokéDex 的应用程序。我正在使用 RecyclerViewer 和 Mike Penz 的 FastItemAdapter。我的 Pokemon 类中有各种变量,其中两个我遇到的问题是 String type 和 String weakness。我的目标是在 strings.xml 文件中使用 HTML 标记来添加字体颜色。在我的 ModelGenerator 类中,我有整个 Pokemon 列表,我从 strings.xml 文件中调用了相应的字符串。口袋妖怪(我没有展示 getter 和 setter)package com.example.pokedex;import android.os.Parcel;import android.os.Parcelable;import androidx.annotation.DrawableRes;public class Pokemon implements Parcelable {    private final int id;    private final String name;    private final String type;    private final String weakness;    private final int drawableResource;    private final String description;    private final String category;    private final String ability;    private final String abilityDescription;    private final String stats;    private final String height;    private final String weight;    private final String gender;    private Pokemon evolution;    public Pokemon(int id, String name, String type, String weakness, @DrawableRes int drawableResource,                   String description, String category, String ability, String abilityDescription,                   String stats, String height, String weight, String gender) {        this.id=id;        this.name = name;        this.type = type;        this.weakness = weakness;        this.drawableResource = drawableResource;        this.description = description;        this.category = category;        this.ability = ability;        this.abilityDescription = abilityDescription;        this.stats = stats;        this.height = height;        this.weight = weight;        this.gender = gender;    }    }    }
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

您实际上可以将 HTML 部分包装在 CDATA 部分中,例如

<string name="grass_poison"><![CDATA[<font color='#53E204'>Grass</font>]]>\u0020<![CDATA[<font color='#EB09FC'>Poison</font>]]></string>

在设置文本使用时,

Html.fromHtml(context.getString(R.string.grass_poison));


查看完整回答
反对 回复 2023-02-16
  • 1 回答
  • 0 关注
  • 194 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号