LibGDX-ImageTextButton-对齐方式

问题描述 投票:0回答:1

我在LibGDX中创建了一个ImageTextButton:

    _rewardedVideo = new Texture(Gdx.files.internal("menus/blue.png"));
    _rewardedVideo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

    _rewardedVideoPressed = new Texture(Gdx.files.internal("menus/bluePressed.png"));
    _rewardedVideoPressed.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

    _symbolRewardedVideo = new Texture(Gdx.files.internal("menus/symbolRewardedVideo.png"));
    _symbolRewardedVideo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

    ImageTextButton.ImageTextButtonStyle styleRewardedVideo = new ImageTextButton.ImageTextButtonStyle();
    styleRewardedVideo.up = new TextureRegionDrawable(new TextureRegion(_rewardedVideo));
    styleRewardedVideo.down = new TextureRegionDrawable(new TextureRegion(_rewardedVideoPressed));
    styleRewardedVideo.imageUp = new TextureRegionDrawable(new TextureRegion(_symbolRewardedVideo));
    styleRewardedVideo.font = skin.getFont("large-font");
    styleRewardedVideo.fontColor = Color.WHITE;

    buttonRewardedVideo = new ImageTextButton("WATCH VIDEO", styleRewardedVideo);
    buttonRewardedVideo.setPosition(580, 180);
    buttonRewardedVideo.setSize(450, 80);
    // The last two lines do not work:
    buttonRewardedVideo.getLabel().setAlignment(Align.left);
    buttonRewardedVideo.getImage().setAlign(Align.right);

我希望按钮将包含一个左对齐的标签和一个右对齐的图像。但这是相反的:https://i.ibb.co/QCZPqT8/Image-Text-Button.png

怎么了,我该怎么解决?

libgdx alignment
1个回答
1
投票

ImageTextButtonTable,如果您查看类,它会先添加Image,然后添加Label ...

public ImageTextButton (String text, ImageTextButtonStyle style) {
    super(style);
    this.style = style;

    defaults().space(3);

    image = new Image();
    image.setScaling(Scaling.fit);

    label = new Label(text, new LabelStyle(style.font, style.fontColor));
    label.setAlignment(Align.center);

    add(image);
    add(label);

    setStyle(style);

    setSize(getPrefWidth(), getPrefHeight());
}

您可以创建自己的类,该类将是ImageTextButton的副本,只需交换这些行或清除表并像这样重新添加项目...

buttonRewardedVideo = new ImageTextButton("WATCH VIDEO", styleRewardedVideo);

buttonRewardedVideo.clearChildren();
buttonRewardedVideo.add(buttonRewardedVideo.getLabel());
buttonRewardedVideo.add(buttonRewardedVideo.getImage());

buttonRewardedVideo.setPosition(580, 180);
buttonRewardedVideo.setSize(450, 80);

© www.soinside.com 2019 - 2024. All rights reserved.