在libgdx 0.9.7中绘制表格边框

15
我正在使用libgdx游戏框架绘制表格,已经成功地实现了我想要的一切渲染效果。现在我只需要显示表格单元格的边框,但是目前为止我还没有找到相关资料。我也尝试使用调试边框线来实现目的,但是也无法更改它们的颜色。
这是我的代码。
Table table = new Table();
table.setFillParent(true);
table.left().top();

table.add(new Label("Results", new LabelStyle(font, Color.BLACK))).colspan(2).expandX();
table.row();
table.add(new Label(text_you_score, new LabelStyle(font, Color.BLACK)));
table.add(new Label(text_actual_score, new LabelStyle(font, Color.BLACK)));
return table;

表格和单元格有许多可用属性,但没有边框属性。我们也可以手动绘制网格线,但我认为这在每个设备分辨率上都不完美。非常感谢任何帮助或想法。

3个回答

8
这是基于libGDX 1.5.3的:
解决方案是使用NinePatch作为Table的背景图片。
NinePatch patch = new NinePatch(new Texture(Gdx.files.internal("background.png")),
     3, 3, 3, 3);
NinePatchDrawable background = new NinePatchDrawable(patch);

Table table = new Table();
table.setBackground(background);

使用数字变量,您可以修改边框半径。确保与 PNG 文件匹配。


3
  1. 注册表格以启用调试模式

    table.debug();

  2. render()方法中调用

    Table.drawDebug(stage);

示例


1
我进行了负投票,因为与下面使用NinePatch的答案相比,使用调试方法明显是获取表格边框的劣质方式。首先,调试方法不允许对边框的宽度或颜色进行任何控制。 - Ryan Stout

-1

我通过从使用 ShapeRenderer 和调用 table.drawDebug(shapeRenderer shapes)(我无法让它正常工作)转换为使用 stage 并将表格添加为演员来实现调试行的显示。

example 中提供了清晰的展示,但这里有一个简短的片段供参考。这是在 libGDX 1.5.3 上进行的。

public class MyGdxGame extends ApplicationAdapter {

    Texture img;
    Stage stage;

    @Override
    public void create () {
        img = new Texture("badlogic.jpg");
        stage = new Stage();
        final Skin skin = new Skin();

        Label.LabelStyle style = new Label.LabelStyle(new BitmapFont(),Color.WHITE);
        TextField.TextFieldStyle textFieldStyle = new TextField.TextFieldStyle();
        textFieldStyle.fontColor = Color.WHITE;
        textFieldStyle.font = new BitmapFont();

        skin.add("default", style);
        skin.add("default", textFieldStyle);
        // Keep your code clean by creating widgets separate from layout.
        Label nameLabel = new Label("Name:", skin);
        TextField nameText = new TextField("",skin);
        Label addressLabel = new Label("Address:", skin);
        TextField addressText = new TextField("",skin);

        Table table = new Table();
        table.add(nameLabel);              // Row 0, column 0.
        table.add(nameText).width(100);    // Row 0, column 1.
        table.row();                       // Move to next row.
        table.add(addressLabel);           // Row 1, column 0.
        table.add(addressText).width(100); // Row 1, column 1.

        table.setOriginX(0);
        table.setOriginY(0);
        table.setX(200);
        table.setY(200);
        table.debug();

        stage.addActor(table);
    }

    @Override
    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

    public void resize (int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    public void dispose () {
        stage.dispose();
    }   
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接