我对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(){}
}
编辑:我意识到像stages和tables这样的父对象控制着它们的子对象的属性,所以我添加了:
table.add(btn).width(300).height(100);
现在它看起来更好了,但是文本偏离了中心。
发布于 2019-04-25 15:49:05
对于文本按钮,应使用setFontScale()和pack()将按钮调整为相应的字体大小。
但是,为了支持不同的屏幕尺寸而不需要自己编码,可以考虑使用FitViewport/ExtendViewport
https://stackoverflow.com/questions/55816846
复制相似问题