简体   繁体   English

在LibGDX中更改文本按钮的文本大小

[英]Change text size of Textbutton in LibGDX

I am new to Libgdx and I have been searching for 3 hours to find a way to change text size in text button but I couldn't find any method for this. 我是Libgdx的新手,我一直在搜索3个小时,以找到一种方法来更改文本按钮中的文本大小,但我找不到任何方法。 What should I do to do this. 我应该怎么做才能做到这一点。

    Table rootTable = new Table();
    rootTable.setFillParent(true);
    rootTable.setDebug(true, true);

    Table menuTable = new Table();
    rootTable.add(menuTable).expand().center().right();

    Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

    TextButton singlePlayer = new TextButton("Single Player",skin);
    TextButton multiPlayer = new TextButton("Multi Player", skin);
    TextButton options = new TextButton("Options", skin);
    TextButton exit = new TextButton("Exit", skin);

    int x = Gdx.graphics.getHeight();
    int unit = x / 6;

    float scale = unit / singlePlayer.getHeight();

    float width = singlePlayer.getWidth() * scale;

    menuTable.add( singlePlayer ).size( width,unit).right().row();
    menuTable.add( multiPlayer ).size( width,unit).right().row();
    menuTable.add( options ).size( width,unit).right().row();
    menuTable.add( exit ).size( width,unit).right().row();

使用textButton.getLabel().setFontScale(x, y)

Use FreeTypeFontGenerator to set a font and then Update the button: 使用FreeTypeFontGenerator设置字体,然后更新按钮:

private void UpdateButton(float xf, float width, float height) {
    if (xf > grayButton.getX() + grayButton.getWidth() / 2) {
        width += 20;
        height += 20;
    } else {
        width -= 20;
        height -= 20;
    }
    params.size = (int) ((int) width / 4.0);
    fontB = generator.generateFont(params);
    grayButtonStyle.font = fontB;
    grayButton.remove();
    grayButton = new TextButton("abcABC", grayButtonStyle);
    grayButton.setSize(width, height);
    grayButton.setPosition((screenWidth - width) / 2, (screenHeight - height) / 2);
    grayButton.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            UpdateButton(Gdx.input.getX(), grayButton.getWidth(), grayButton.getHeight());
        }
    });
    stage.addActor(grayButton);
}

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

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