TextButton中的位置标签?

时间:2014-10-04 21:37:12

标签: java libgdx scene2d

我想将TextButton用于自定义按钮,但这要求我({1}}或textLabel内进行(重新)定位。我尝试过一些选择,但我很困惑。

TextButton

我将//this seems straightforward but nothing changes. final TextButton b = new TextButton("LABEL", skin); b.getLabel().setPosition(100, -20); //Read somewhere about adding new children to buttons but no success either. final TextButton b = new TextButton("OLD LABEL", skin); b.clearChildren(); Label l = new Label("NEW LABEL", skin); l.setPosition(400, 600); b.add(l); b.layout(); // Does not help either. 直接添加到Button中,没有关于重要事项的表格。任何人对此都有一些想法吗?

enter image description here

2 个答案:

答案 0 :(得分:0)

texbutton是一个按钮,他们在构造函数中添加了一个标签,

label = new Label(text, new LabelStyle(style.font, style.fontColor));
label.setAlignment(Align.top);
add(label).expand().fill();

首先是有或没有看到任何方法来改变标签的对齐方式。

label.setAlignment(Align.top);
另一方面,

将标签添加到新单元格中,即演员

add(label).expand().fill();

知道我发现的唯一事情是实施一些房子,因为我展示的不是很好,但我想你会

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;

import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.esotericsoftware.tablelayout.Cell;


public class LabelButton extends Button {

    private final Label label;
    private TextButtonStyle style;
    private float widthButton, heightButton;

    public LabelButton (String text, Skin skin) {
        this(text, skin.get(TextButtonStyle.class));
        setSkin(skin);
    }
    //NEW CODE1 FOR ADD, NEW CLASS EXTENDS BUTTON, OTHER CODE EQUALS TEXTBUTTON.
    /** @param boolean alignament: false desactiva la alineación al centro, x e y son las coordenadas donde se mostrar el texto del label. 
      * <p>
      * xLabel, e yLabel tienen su pivote en el centro del texto, asi que las posiciones son basadas respecto a esto.
      * <p>
      * las variables float widthButton, y float heightButton, referidos a el boton.
      * <p>
      * boolean alignament: false deactivate center alignment, x and y are the coordinates where show the text label. 
      * <p> 
      * xLabel, ylabel and have its pivot in the center of the text, so the positions are based on this.
      * <p>
      * las variables widthButton float and float heightButton, referring to the button
      */

    public LabelButton (String text, Skin skin, boolean alignament, float xLabel, float yLabel, float widthButton, float heightButton) {
        this(text, skin.get(TextButtonStyle.class), alignament, xLabel, yLabel, widthButton, heightButton);
        setSkin(skin);
    }
    //END NEW CODE1

    public LabelButton (String text, Skin skin, String styleName) {
        this(text, skin.get(styleName, TextButtonStyle.class));
        setSkin(skin);
    }

    public LabelButton (String text, TextButtonStyle style) {
        super();
        setStyle(style);
        this.style = style;
        label = new Label(text, new LabelStyle(style.font, style.fontColor));
        label.setAlignment(Align.top);
        add(label).expand().fill();
        setSize(getPrefWidth(), getPrefHeight());
    }
    //NEW CODE2 FOR ADD, NEW CLASS EXTENDS BUTTON, OTHER CODE EQUALS TEXTBUTTON.
    /** @param boolean alignament: false desactiva la alineación al centro, x e y son las coordenadas donde se mostrar el texto del label. 
      * <p>
      * xLabel, e yLabel tienen su pivote en el centro del texto, asi que las posiciones son basadas respecto a esto.
      * <p>
      * las variables float widthButton, y float heightButton, referidos a el boton.
      * <p>
      * boolean alignament: false deactivate center alignment, x and y are the coordinates where show the text label. 
      * <p> 
      * xLabel, ylabel and have its pivot in the center of the text, so the positions are based on this.
      * <p>
      * las variables widthButton float and float heightButton, referring to the button
      */

    public LabelButton (String text, TextButtonStyle style, boolean alignament, float xLabel, float yLabel, float widthButton, float heightButton) {
        super();
        setStyle(style);
        this.style = style;
        label = new Label(text, new LabelStyle(style.font, style.fontColor));
        if (alignament == true){
            label.setAlignment(Align.top);
            add(label).expand().fill();
        }else{
            this.heightButton = heightButton;
            this.widthButton = widthButton;
            add(label).padRight(widthButton - (xLabel * 2)).padBottom(-heightButton + (yLabel * 2));        
        }

        setSize(getPrefWidth(), getPrefHeight());

    }

    public void setCoordenadasLabel(float xLabel, float yLabel){
        getCell(label).padRight(widthButton - (xLabel * 2)).padBottom(-heightButton + (yLabel * 2));
    }
    //END NEW CODE2

    public void setStyle (ButtonStyle style) {
        if (style == null) {
            throw new NullPointerException("style cannot be null");
        }
        if (!(style instanceof TextButtonStyle)) throw new IllegalArgumentException("style must be a TextButtonStyle.");
        super.setStyle(style);
        this.style = (TextButtonStyle)style;
        if (label != null) {
            TextButtonStyle textButtonStyle = (TextButtonStyle)style;
            LabelStyle labelStyle = label.getStyle();
            labelStyle.font = textButtonStyle.font;
            labelStyle.fontColor = textButtonStyle.fontColor;
            label.setStyle(labelStyle);
        }
    }

    public TextButtonStyle getStyle () {
        return style;
    }

    public void draw (Batch batch, float parentAlpha) {
        Color fontColor;
        if (isDisabled() && style.disabledFontColor != null)
            fontColor = style.disabledFontColor;
        else if (isPressed() && style.downFontColor != null)
            fontColor = style.downFontColor;
        else if (isChecked() && style.checkedFontColor != null)
            fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;
        else if (isOver() && style.overFontColor != null)
            fontColor = style.overFontColor;
        else
            fontColor = style.fontColor;
        if (fontColor != null) label.getStyle().fontColor = fontColor;
        super.draw(batch, parentAlpha);
    }

    public Label getLabel () {
        return label;
    }

    public Cell getLabelCell () {
        return getCell(label);
    }

    public void setText (String text) {
        label.setText(text);
    }

    public CharSequence getText () {
        return label.getText();
    }

    /** The style for a text button, see {@link TextButton}.
     * @author Nathan Sweet */
    static public class TextButtonStyle extends ButtonStyle {
        public BitmapFont font;
        /** Optional. */
        public Color fontColor, downFontColor, overFontColor, checkedFontColor, checkedOverFontColor, disabledFontColor;

        public TextButtonStyle () {
        }

        public TextButtonStyle (Drawable up, Drawable down, Drawable checked, BitmapFont font) {
            super(up, down, checked);
            this.font = font;
        }

        public TextButtonStyle (TextButtonStyle style) {
            super(style);
            this.font = style.font;
            if (style.fontColor != null) this.fontColor = new Color(style.fontColor);
            if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor);
            if (style.overFontColor != null) this.overFontColor = new Color(style.overFontColor);
            if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor);
            if (style.checkedOverFontColor != null) this.checkedFontColor = new Color(style.checkedOverFontColor);
            if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor);
        }
    }   
}

这些是您想要的新方法。

public LabelButton (String text, TextButtonStyle style, boolean alignament, float xLabel, float yLabel, float widthButton, float heightButton) {
        super();
        setStyle(style);
        this.style = style;
        label = new Label(text, new LabelStyle(style.font, style.fontColor));
        if (alignament == true){
            label.setAlignment(Align.top);
        }else{
            this.heightButton = heightButton;
            this.widthButton = widthButton;
        }

        add(label).padRight(widthButton - (xLabel * 2)).padBottom(-heightButton + (yLabel * 2));
        setSize(getPrefWidth(), getPrefHeight());   
    }

    public void setCoordenadasLabel(float xLabel, float yLabel){
        getCell(label).padRight(widthButton - (xLabel * 2)).padBottom(-heightButton + (yLabel * 2));
    }

简单的例子

.//

LabelBoton labelBoton = new LabelButton("hola", skinMenuPrincipal, false, 150, 150, 300, 300);
labelBoton.setBounds(50, 50, 300, 300);

stage.addActor(labelBoton);

在构造函数中,它仍然和以前一样,但是使用不与中心对齐的布尔值false,前两个float是您希望文本出现的位置,但是pivot是此中心的中心角落,和过去的两个浮动

你必须说出你要有按钮才能进行计算的高度和宽度,然后不要使用setPosition标签在单元格中而我不做,所以我有管理允许的内容我是单元格,然后执行具有相同高度和宽度的setBounds

这个其他方法允许您移动标签,而不是更改您创建的按钮的高度和宽度

setCoordenadasLabel(float xLabel, float yLabel);

还告诉你谁不是精确100%,但我认为你一方面是值得的,那么你必须打开文件uiskin.json并添加这个

com.mygdx.game.LabelButton$TextButtonStyle: {
default: { down: default-round-down, up: default-round, font: default-font, fontColor: white },
toggle: { down: default-round-down, up: default-round, checked: default-round-down, font: default-font, fontColor: white, downFontColor: red }
},

com.mygdx.game已被替换。按此类

的新包名称

我希望你工作并成为你想要的,我证明它有效,但如果出现一些错误就不行,如果我没有表达清楚的话,对不起我的英语,再见。

其他方式

另一种方法是只使用,而不扩展类,但对于各种按钮,如果你必须在应用程序期间修改或更多精度,我认为最好使用扩展类。

TEXTBUTTON.getCell(label).padRight("YOUR POSITION").padBottom("YOUR POSITION);

答案 1 :(得分:0)

解决方案很简单:

final TextButton b = new TextButton("LABEL", skin);
b.getLabelCell().padBottom(xxx);