的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);
按鈕是一個表的子類。可以直接調用'textButton.top()。right()。padTop(20).padRight(20);'。如果這不起作用,那麼9補丁可能會有所幫助。 – Tenfour04 2014-10-07 20:17:34
如果您需要根據字符串將矩形區域展開爲各種大小,請使用9貼片作爲背景。實際上,僅9個補丁就可以解決這個問題。在Android用戶界面中,9貼片可以定義圖像的哪一部分超出文本將要去的區域。不知道它在Libgdx中的行爲是否相同。 – Tenfour04 2014-10-07 20:22:16
看看這裏: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