简体   繁体   English

调整TextButton LibGDX的大小

[英]Resizing TextButton LibGDX

I'm kinda new to LibGDX. 我是LibGDX的新手。 I'm just learning the ropes. 我只是在学习绳索。 Trying to get a basic TextButton to display at a reasonable size on screen. 试图让一个基本的TextButton在屏幕上以合理的大小显示。 Working exclusively on Android IDE (AIDE). 专门在Android IDE(AIDE)上工作。 Using only default skins and what-not, I've tried to set width and height as well as setScale() but only the text size seems to increase on the command. 我只使用默认皮肤和什么不是,我试图设置宽度和高度以及setScale(),但只有文本大小似乎在命令上增加。 What am I doing wrong? 我究竟做错了什么?

https://oi1161.photobucket.com/albums/q501/StudioGilliam/Screenshot_20190423-184819_LibGDXButtonsTest.jpg https://oi1161.photobucket.com/albums/q501/StudioGilliam/Screenshot_20190423-184819_LibGDXButtonsTest.jpg

public class MyGdxGame implements ApplicationListener
{
   private Stage stage;
   private Table table;
   private TextButton btn;

   @Override
   public void create()
   {
      stage = new Stage(new ScreenViewport());
      Gdx.input.setInputProcessor(stage);

      table = new Table();
      table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

      TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
      style.font = new BitmapFont();

      btn = new TextButton("Button", style);
      btn.pad(20);
      btn.setTransform(true);
      btn.setScale(5.0f);
      btn.setTouchable(Touchable.enabled);

      table.add(btn);
      table.debug();

      stage.addActor(table);

   }

   @Override
   public void render()
   {        
    Gdx.gl.glClearColor(0, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      stage.act();
      stage.draw();
   }

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

   @Override
   public void resize(int width, int height){}

   @Override
   public void pause(){}

   @Override
   public void resume(){}
}

EDIT: I realised that parent objects like stages and tables control the properties of their children, so I added: 编辑:我意识到像stage和tables这样的父对象控制着他们孩子的属性,所以我补充说:

table.add(btn).width(300).height(100);

And now it looks better but the text is way off centre... 现在它看起来更好,但文字偏离中心......

https://oi1161.photobucket.com/albums/q501/StudioGilliam/Screenshot_20190423-191104_LibGDXButtonsTest.jpg https://oi1161.photobucket.com/albums/q501/StudioGilliam/Screenshot_20190423-191104_LibGDXButtonsTest.jpg

On a Textbutton, you should use setFontScale() and pack() to resize the button to the font size. 在Textbutton上,您应该使用setFontScale()和pack()将按钮的大小调整为字体大小。

However, for supporting different screen sizes without any own coding, consider using FitViewport/ExtendViewport 但是,为了支持不同的屏幕尺寸而不进行任何编码,请考虑使用FitViewport / ExtendViewport

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM