LibGDX - 获取ImageTextButton中标签单元格的宽度

问题描述:

我在容器表中有ImageTextButtons。我想动态地改变这个表的宽度并让ImageTextButton以特定的方式响应。我希望他们做的是根据所显示文本的百分比淡入标签文本。LibGDX - 获取ImageTextButton中标签单元格的宽度

我相信我正确地动态设置表格宽度和内容。根据LibGDX文档,我们不应该明确地设置窗口小部件的尺寸,而是表格(容器)的单元尺寸。所以对于测试,我对这样的按钮有一个简单的clickListener;

button.addListener(new ClickListener(){ 
     @Override 
     public void clicked(InputEvent event, float x, float y) { 
      super.clicked(event, x, y); 
      float max = container.getCell(button).getMaxWidth(); 
      container.getCell(button).width(max*0.9f); 
      container.invalidate(); 
     } 
    }); 

这工作正常,我点击它,单元格(和小部件)每次缩小90%。在我的自定义ImageTextButton的draw()方法中,我认为这将是简单的,

glyphWidth = getLabel().getGlyphLayout().width; 
    labelCellWidth = getLabelCell().getMinWidth(); ///<--problem here 

    float glyphOpacityMultiplier = MathUtils.clamp(labelCellWidth/glyphWidth, 0f, 1f); 

    origLabelColor = getLabel().getColor().cpy(); 
    newLabelColor = origLabelColor.cpy(); 
    newLabelColor = new Color(newLabelColor.r, newLabelColor.g, newLabelColor.b, newLabelColor.a*glyphOpacityMultiplier); 

    getLabel().setColor(newLabelColor); 
    super.draw(batch, parentAlpha); 
    getLabel().setColor(origLabelColor); 

这一切的工作,除了我找不到任何方法给我的实际宽度的单元格包含标签。由于ImageTextButton本身也是一个Table,所以我试图在ClickListener中做button.invalidate(),但它没有帮助。这些是我迄今尝试过的方法;

getLabelCell().getMinWidth(); 
    getLabelCell().getMaxWidth(); 
    getLabelCell().getPrefWidth(); 
    getLabelCell().getActorWidth(); 
    getLabelCell().getSpaceLeft(); 
    getLabelCell().getSpaceRight(); 

他们都不更新,并给我正确的大小,但我知道什么是工作的地方,因为我打开的ImageTextButton“setClip的(真)”,并在标签文本已成功被内修剪NinePatch背景在调整大小时的边界。那么如何获得正确的尺寸?

任何帮助,非常感谢!

糟糕..解决了它。我认为包含标签的单元格将被NinePatch背景抵消和确定大小,但它不是,它是全尺寸但具有填充。因此答案是:

labelCellWidth = getWidth()-getPadLeft()-getPadRight();