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

时间:2019-06-24 16:35:55

标签: java android

我正在创建类似PokéDex的应用程序。我正在使用带有Mike Penz的FastItemAdapter的RecyclerViewer。我的Pokemon类中有多个变量,但我遇到的两个问题是String类型和String弱点。

我的目标是在strings.xml文件中使用HTML标记以添加字体颜色。

在ModelGenerator类中,在其中包含了完整的Pokemon列表,我从strings.xml文件中调用了相应的字符串。

Pokemon(我没有展示吸气剂和吸气剂)

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];
        }
    };

strings.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>

ModelGenerator(为简单起见,我只展示一个口袋妖怪)

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中设置类型和弱点时,它们会显示出来,但不会显示任何颜色。 我想念什么?

1 个答案:

答案 0 :(得分:0)

您实际上可以将CDATA部分中的HTML部分包装起来

<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));