【问题标题】:Drawing table borders in libgdx 0.9.7在 libgdx 0.9.7 中绘制表格边框
【发布时间】:2012-11-29 11:42:45
【问题描述】:

我正在使用 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;

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

【问题讨论】:

    标签: java opengl-es libgdx


    【解决方案1】:

    这是基于 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 文件匹配。

    【讨论】:

      【解决方案2】:
      1. 为您的表注册调试模式

        table.debug();

      2. 调用render()方法

        Table.drawDebug(stage);

      Example

      【讨论】:

      • 我投了反对票,因为与下面使用 NinePatch stackoverflow.com/a/28728932/2251083 的答案相比,使用调试方法显然是获取表格边框的次要方法。一方面,调试方法不允许对边框的宽度或颜色进行任何控制。
      【解决方案3】:

      我通过使用ShapeRenderertable.drawDebug(shapeRenderer shapes) 调用(我无法开始工作)转换为使用stage 并将表添加为演员,从而实现了调试行的显示。

      上面答案中链接的example 确实清楚地表明了这一点,但这里有一个简短的 sn-p 供参考。这是在 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();
          }   
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-17
        • 2023-03-20
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-10
        相关资源
        最近更新 更多