2016-06-09 143 views
0

大家好我正在尝试使用Libgdx中的Table来显示一些图像,但它没有正确显示它们,我无法找到修复该问题的方法 当我使用table.bottom时,它会显示它们,但当我使用table.center它显示他们较低,当我选择上,左或右它说明不了什么 :这里是我的代码格式化表格:Libgdx表组件无法正确显示

public class MenuIcons { 
    public Viewport viewport; 
    public Stage stage; 
    public boolean upPressed; 
    public boolean leftPressed; 
    public boolean rightPressed; 
    public boolean levellock1,levellock2; 
    public Image lock1; 
    public Image lock2; 

    public OrthographicCamera camera; 
    public Table table; 

    //Constructor. 
    public MenuIcons(SpriteBatch spriteBatch) { 
     camera = new OrthographicCamera(); 
     viewport = new FitViewport(400, 208, camera); 
     stage = new Stage(viewport, spriteBatch); 
     Gdx.input.setInputProcessor(stage); 
     initalizeTable(); 


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

    public void resize(int width, int height) { 
     viewport.update(width, height); 
    } 
    public void initalizeTable() 
    { 
     //Buttons with images. 
     lock1 = new Image(new Texture("levellock.png")); 
     lock1.setSize(25, 25); 
     lock1.addListener(new InputListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       levellock1 = true; 
       return true; 
      } 

      @Override 
      public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       levellock1 = false; 
      } 
     }); 
     lock2 = new Image(new Texture("levellock.png")); 
     lock2.setSize(25, 25); 
     lock2.addListener(new InputListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       levellock2 = true; 
       return true; 
      } 

      @Override 
      public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       levellock2 = false; 
      } 
     }); 
     //Table with buttons. 
     table = new Table(); 
     table.bottom; //Align to the left bottom. 
     table.add(); 
     table.add(); 
     table.add(); 
     table.add(lock1).size(lock1.getWidth(), lock1.getHeight()); 
     table.add(lock2).size(lock2.getWidth(), lock2.getHeight()); 



     stage.addActor(table); 
    } 

    public boolean isLevellock1() { 
     return levellock1; 
    } 

    public boolean isLevellock2() { 
     return levellock2; 
    } 

} 

这里是图像,当我使用Table.bottom

这里是图像,当我使用Table.center

回答

1

我建议通过信息和例子在这里阅读: https://github.com/libgdx/libgdx/wiki/Table

此行甚至不会工作?:table.bottom;

试试这个table.left().bottom();而不是table.bottom;

或者更好的是你可以对齐单个项目,当你添加它们,这样的事情:你为什么反复使用空白table.add();

table.add(lock1).width(lock1.getWidth()).height(lock1.getHeight()).left().bottom();

注意事项?你不应该用table.row()代替吗?


编辑发表评论:

你需要创建一个行或空间,为您的项目,以适应,而不是使用table.add();。你正在造成各种各样的问题。

在你走之前的任何进一步你只需要table = new Table();后添加此table.setDebug(true);所以你可以看到你在做什么。

现在试试这个:

table = new Table(); 
    table.setDebug(true); 
    table.top().left(); 
    table.add(lock1).width(lock1.getWidth()).height(lock1.getHeight()).left().top(); 
    table.row(); 
    table.row(); 
    table.add(lock2).width(lock2.getWidth()).height(lock2.getHeight()).left().bottom(); 

现在你可以看到调试线,你可以看到你要去哪里错了。在table.row();之间添加一个空白标签,并且使用.expand();使其填满的空间是这样的:在根表

table.row(); 
    table.add(myBlankLabel).expandY(); 
    table.row(); 
+0

我希望它是table.left()。但它并没有工作。 我使用table.add在它们之间创建一些空格 –

+1

您确实需要阅读我的消息开头链接的文档,因为它具有所有答案。看看我的编辑:你需要添加这个'table.setDebug(true);'所以你可以看到发生了什么。 – sorifiend

+0

谢谢,完成了 –

1

呼叫table.setFillParent(true)。否则,像table.bottom()这样的调用就没有意义,因为逻辑表正在一个零大小的框中对齐。