Libgdx Scene2d - 设置actor(TextField)填充?

che*_*o_c 5 user-interface android padding textfield libgdx

我一直无法设置填充或类似于演员的东西.无法弄明白的方式.我想我必须在皮肤上添加一些东西吗?

我有这个TextField:

        textboxskin = new Skin();
        textboxskin.add("textfieldback", new Texture("data/textfieldback.png"));
        textboxskin.add("cursor", new Texture("data/cursortextfield.png"));
        textboxskin.add("selection", new Texture("data/selection.png"));
        textboxskin.add("font", font);

        TextFieldStyle textfieldstyle = new TextFieldStyle();
        textfieldstyle.background= textboxskin.getDrawable("textfieldback");
        textfieldstyle.disabledFontColor=Color.BLACK;
        textfieldstyle.font=textboxskin.getFont("font");
        textfieldstyle.fontColor=Color.WHITE;
        textfieldstyle.cursor=textboxskin.getDrawable("cursor");
        textfieldstyle.selection=textboxskin.getDrawable("selection");  


        textfieldusername = new TextField("username", textfieldstyle);
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

在此输入图像描述

你可以看到它看起来很可怕左中心...

小智 11

编辑:我试图做以下,它在我的测试中工作:

textfieldstyle.background.setLeftWidth(textfieldstyle.background.getLeftWidth() + 10);
Run Code Online (Sandbox Code Playgroud)

搜索libgdx API我找不到为TextField组件内的文本指定填充的方法.

作为一种解决方法,您可以复制原始TextField源并创建一个新的TextField,另一个名称提供了实现填充的方法.

将libgdx TextField(https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java?source=cc)的内容复制到另一个文件并将其命名为MyTextField(作为示例).用新的类名替换损坏的TextField引用.

创建一个名为paddingLeft的新变量,例如:

public float paddingLeft = 0.0f;
Run Code Online (Sandbox Code Playgroud)

在draw()方法中,您可以将填充添加到总和以定义String的新位置.代码所在的位置:

font.draw(batch, displayText, x + bgLeftWidth + textOffset, y + textY + yOffset, visibleTextStart, visibleTextEnd);
Run Code Online (Sandbox Code Playgroud)

用...来代替:

font.draw(batch, displayText, x + bgLeftWidth + textOffset + leftPadding, y + textY + yOffset, visibleTextStart, visibleTextEnd);
Run Code Online (Sandbox Code Playgroud)

(注意第二个代码中的"+ leftPadding")

然后,如果您正在使用皮肤,请记住在uiskin.json文件中引用您的TextField:

mypackage.MyTextField$TextFieldStyle: {
    default: { selection: selection, background: textfield, font: default-font, fontColor: white, cursor: cursor }
}
Run Code Online (Sandbox Code Playgroud)

并在您的代码中使用它:

MyTextField txtTest = new MyTextField("Test", skin);
txtTest.leftPadding = 10.0f;
Run Code Online (Sandbox Code Playgroud)

这不是理想的方式,但会起作用.


Ian*_*itz 2

使用 Table 类来布局 scene2d UI。设置表:

stage = new Stage();
Table table = new Table();
table.setFillParent(true);
stage.addActor(table);
table.add(textFieldUsername).padBottom(20f); //also use padTop, padLeft, and padRight
table.row();
Run Code Online (Sandbox Code Playgroud)

在主循环中,调用:

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
Run Code Online (Sandbox Code Playgroud)

有关表格的更多信息,请参阅:http ://code.google.com/p/table-layout/