如何设置 libgdx 列表中特定行的颜色?

How can I set the color of a specific line in a libgdx list?

我正在开发一个列表,我想通过将字体颜色显示为红色来突出显示某些行。这可能吗?

编辑: 好的,这是我的代码的简化版本:

Skin defListSkin = new Skin(Gdx.files.internal("data/uiskin.json"));

List listHistory = new List<String>(defSkin);

// Here I set the general font color for the list
List.ListStyle listStyle = listHistory.getStyle();
listStyle.font = fontList;
listStyle.fontColorUnselected = Color.LIGHT_GRAY;
listHistory.setStyle(listStyle);

String[] items = new String[20];

// Example of item[]
// item[0]: "John   12"
// item[1]: "Amy    -3" <-- I want certain lines to appear in red (e.g. those with negative numbers)

// Populate the list
listHistory.setItems(items);

// Drawing the list (actual draw happens in render() of course)
Table myTable = new Table();
myTable.add(listHistory);
stage.addActor(myTable);

我明白你的意思,我认为这是不可能的。列表项都链接到相同的 listStyle,更改列表项颜色的唯一方法是为 listHistory 制作一个新样式并更改此样式的字体颜色,但是如果您这样做,它会更改每个项目在列表中添加新的字体颜色。我的建议是创建两个单独的列表和两个单独的 ListStyles 并且您希望红色的行在一个列表中并具有一种样式,而您不想红色的行在另一个列表中并具有不同的样式。希望这有帮助

使用Libgdx支持的颜色标记语言。

标记语法非常简单但用途广泛:

[name] 按名称设置颜色。有一些预定义的颜色,详尽列表请参见 Colors.reset() 方法。用户可以通过 Colors class 的方法定义自己的颜色。 [#xxxxxxxx] 设置由十六进制值 xxxxxxxx 指定的颜色,格式为 RRGGBBAA,其中 AA 是可选的,默认为 0xFF。 [] 将颜色设置为以前的颜色(一种可选的结束标记) [[ 转义左括号。

默认情况下禁用标记。使用 public 成员 font.getData().markupEnabled 将其变为 on/off.

参考:https://github.com/libgdx/libgdx/wiki/Color-Markup-Language

如果您想要更复杂的 html 标记,您可以使用这些模板:https://github.com/czyzby/gdx-lml, live tests: http://czyzby.github.io/gdx-lml-tests/