我正在创建一个类似 PokéDex 的应用程序。我正在使用 RecyclerViewer 和 Mike Penz 的 FastItemAdapter。我的 Pokemon 类中有各种变量,其中两个我遇到问题的是 String 类型和 String 弱点。
我的目标是在 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;
}
protected Pokemon(Parcel in) {
id = in.readInt();
name = in.readString();
type = in.readString();
weakness = in.readString();
drawableResource = in.readInt();
description = in.readString();
category = in.readString();
ability = in.readString();
abilityDescription = in.readString();
stats = in.readString();
height = in.readString();
weight = in.readString();
gender = in.readString();
evolution = in.readParcelable(Pokemon.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeString(type);
dest.writeString(weakness);
dest.writeInt(drawableResource);
dest.writeString(description);
dest.writeString(category);
dest.writeString(ability);
dest.writeString(abilityDescription);
dest.writeString(stats);
dest.writeString(height);
dest.writeString(weight);
dest.writeString(gender);
dest.writeParcelable(evolution, flags);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Pokemon> CREATOR = new Creator<Pokemon>() {
@Override
public Pokemon createFromParcel(Parcel in) {
return new Pokemon(in);
}
@Override
public Pokemon[] newArray(int size) {
return new Pokemon[size];
}
};
字符串.xml
<resources>
<string name="grass_poison"><font color='#53E204'>Grass</font>\u0020<font color='#EB09FC'>Poison</font></string>
<string name="b_i_v_weakness"><font color='#FF0000'>Fire</font>\u0020<font color='#7158FC'>Flying</font>\u0020<font color='#1BE4EB'>Ice</font>\u0020<font color='#E91E63'>Psychic</font></string>
</resources>
模型生成器(为了简单起见,我只显示了一个神奇宝贝)
package com.example.pokedex;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
public class ModelGenerator {
public static List < Pokemon > getPokemons(Context context) {
List < Pokemon > pokemons = new ArrayList < > ();
Pokemon bulbasaur = new Pokemon(1, "Bulbasaur", context.getString(R.string.grass_poison), context.getString(R.string.b_i_v_weakness),
R.drawable.bulbizarre, context.getString(R.string.bulbizarre_description), "Seed", "Overgrow",
context.getString(R.string.overgrow_description),
"HP : 2 \nAttack : 3 \nDefense : 2 \nSpecial Attack : 3 \nSpecial Defense : 3 \nSpeed : 3",
"62 cm", "7 kg", "♂ | ♀");
pokemons.add(bulbasaur);
}
}
当我在各自的 TextView 中设置类型和弱点时,它们会显示出来,但不会以任何颜色显示。 我错过了什么?
最佳答案
您实际上可以将 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));
关于java - 如何使用 HTML 标签为 strings.xml 文件中的字符串着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740717/