2017-05-30 25 views
0

我想在框圖像中寫入分數。以正確的方式對齊圖像中的標籤文本-RefGdx

首先我把盒子放在那個地方,然後我用這樣的標籤在它上面添加了樂譜字符串。

private void drawScoreBoxes() { 
    scoreImage = new Image(scoreTexture); 
    stage.addActor(scoreImage); 
    scoreImage.setPosition(UiConstants.BOX_POS_X,UiConstants.BOX_POS_Y2); 
} 

private void drawScore() { 

     Label.LabelStyle scoreStyle = new Label.LabelStyle();// style 
     scoreStyle.font = game.font2; 
     scoreLabel2 = new Label(scoreController.getScoreString(), scoreStyle);// label 
     scoreLabel2.setPosition(scoreImage.getX()+scoreImage.getWidth()/2,scoreImage.getY());  
    } 

分數線應該來正好在框中image.In的這種情況下,中間,當分數增加位數的數字,字符串定位是​​不妥當的。

我該如何將其中心始終對齊?

我找到像getLabelAlign(),setAlignment(alignment)等方法。 但我不知道如何正確使用它。

回答

1

你有兩個選擇: 1.使用TEXT按鈕:

Drawable scoreDrawable = new TextureRegionDrawable(new TextureRegion(scoreTexture)); 

TextButtonStyle textButtonStyle = new TextButtonStyle(scoreDrawable, scoreDrawable, scoreDrawable, font); 

TextButton tb = new TextButton(scoreController.getScoreString()); 

tb.setDisabled(true); 

或者2.設置標籤邊界和對齊文本:

LabelStyle labelStyle = new LabelStyle(font, Color.BLACK); 

    Label scoreLabel = new Label(scoreController.getScoreString(), labelStyle); 

    scoreLabel.setBounds(scoreImage.getX(), scoreImage.getY(), scoreImage.getWidth(), scoreImage.getHeight()); 

    scoreLabel.setAlignment(Align.center); 
1

使用TextButton,而不是Image + Label

TextButton是包含Label一個按鈕,你可以通過Image繪製的部分按鈕顯示爲您的標籤的背景部分。

button = new TextButton("Button1", textButtonStyle); 
stage.addActor(button); 

TextButton採用一個字符串來渲染和的ButtonStyle,在這種情況下TextButtonStyle,這基本上是一個包含所有有關按鈕(字體,可繪製渲染而沒有按下的信息的類,可拉伸以使同時按下等)。