如何在libgdx中的对话框的右上角添加按钮? [英] How can I add button to top right corner of a Dialog In libgdx?

查看:122
本文介绍了如何在libgdx中的对话框的右上角添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在对话框的右上角添加关闭按钮.

I want to add close button to top right corner of a dialog box.

我尝试将setbounds与addactor一起使用,并且仅使用setsize和addactor进行添加和设置,但是没有任何效果.我知道对话框适用于表格布局,它具有用于内容和按钮的表格.我不想使用此布局,而是将按钮放在此布局之外,就像在对话框的边框上一样.

I tried using setbounds with addactor and just add and setposition with setsize and addactor, but nothing works. I know that dialog works with table layout, it has a table for content and for buttons. I don't want to use this layout and put the button outside this layout like on the border of the dialog.

我该怎么办?

这应该是这样的:

推荐答案

我现在能想到的最简单的解决方案是对按钮使用负填充以将其移到单元格的外部.

The easiest solution I could come up with now, is to use negative padding for your button to move it "outside" of it's cell.

Button closeButton = new TextButton("X", skin, "default");
getTitleTable().add(closeButton).size(60, 40).padRight(-30).padTop(-20);

使用此填充技巧时,您会遇到问题,该按钮将位于对话框的外部,并且默认情况下,Window在执行Actor.hit(...)评估时会检查窗口的边界.

With this padding hack you have the problem, that the button will be outside of your Dialog, and by default, Window checks the bounds of your window when it performs Actor.hit(...) evaluation.

由于这个原因,我们需要禁用裁剪,但是窗口的渲染取决于它.这就是为什么我们使用另一个hack来启用它,仅用于渲染的原因:

We need to disable clipping for that reason, but the rendering of the window depends on it. That's why we use another hack to enable it, just for the rendering:

@Override
public void draw(Batch batch, float parentAlpha) {
    setClip(true);
    super.draw(batch, parentAlpha);
    setClip(false);
}

这篇关于如何在libgdx中的对话框的右上角添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆