【问题标题】:How can I set the color of a specific line in a libgdx list?如何设置 libgdx 列表中特定行的颜色?
【发布时间】:2016-03-18 19:15:34
【问题描述】:

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

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

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

【问题讨论】:

  • 太含糊了 :) 给我们你尝试过的东西,也许是你想要达到的目标的屏幕截图。
  • 你现在画的怎么样了?你有某种List&lt;String&gt;。你可以做spriteBatch.setColor(..)

标签: list libgdx scene2d


【解决方案1】:

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

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

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

默认情况下禁用标记。使用公共成员 font.getData().markupEnabled 开启/关闭。

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

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

【讨论】:

  • 非常好——它有效!我不知道颜色标记语言。谢谢你。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多