2014-10-04 143 views
1

我想为我的自定义按钮使用TextButton,但这需要我(重新)将textLabel定位在该TextButton之内。我尝试了一些选择,但我很困难。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直入Stage没有表什么重要的。任何人对此有一些想法?

enter image description here

+0

按钮是一个表的子类。可以直接调用'textButton.top()。right()。padTop(20).padRight(20);'。如果这不起作用,那么9补丁可能会有所帮助。 – Tenfour04 2014-10-07 20:17:34

+0

如果您需要根据字符串将矩形区域展开为各种大小,请使用9贴片作为背景。实际上,仅9个补丁就可以解决这个问题。在Android用户界面中,9贴片可以定义图像的哪一部分超出文本将要去的区域。不知道它在Libgdx中的行为是否相同。 – Tenfour04 2014-10-07 20:22:16

+0

看看这里:http://javagamexyz.blogspot.de/2013/05/user-interface-menus-with-scene2dui-and.html大概你可以找到一些有用的东西。这个方法对于你在该页面的src代码中很有用:public void addButton(String label,String secondaryLabel,ChangeListener listener,boolean active)....我设法在[x和y]中都有正确的标签位置,I可以发布一些代码,如果需要... – dawez 2014-10-13 09:24:08

回答

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,不对齐到中心,前两个浮动是你想要文本出现的地方,但是枢轴是这个角落的中心,而过去的两个浮动则需要说明你将在orde中使用按钮的高度和宽度r做计算,然后不要使用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); 
+0

我只需要能够移动按钮内的文本。假设我的大按钮是300x200,我的文本/标签只有120x40需要精确定位在左上角,像素精度。 – Madmenyo 2014-10-05 13:58:17

+0

我不太在乎一张桌子,不管有没有好。我非常适合创建表格,菜单和用户界面。我只需要在文本按钮内移​​动文本。 – Madmenyo 2014-10-05 13:59:53

+0

我添加了一张图片来帮助你理解。 – Madmenyo 2014-10-05 14:11:12

0

解决方案很简单:

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