我对 LibGDX 有点陌生。 我只是在学习诀窍。 尝试让基本的 TextButton 以合理的尺寸在屏幕上显示。 专门在 Android IDE (AIDE) 上工作。 仅使用默认皮肤和其他东西,我尝试设置宽度和高度以及 setScale() 但似乎只有文本大小在命令上增加。 我做错了什么?
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(){}
}
编辑: 我意识到父对象(例如阶段和表格)控制其子对象的属性,因此我添加了:
table.add(btn).width(300).height(100);
现在看起来好多了,但文字偏离了中心......
最佳答案
在文本按钮上,您应该使用 setFontScale() 和 pack() 将按钮大小调整为字体大小。
但是,为了支持不同的屏幕尺寸而无需任何自己的编码,请考虑使用 FitViewport/ExtendViewport
关于java - 调整文本按钮 LibGDX 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55816846/