【问题标题】:Libgdx Scene2d - Set actor ( TextField ) padding?Libgdx Scene2d - 设置演员( TextField )填充?
【发布时间】:2014-01-14 05:34:06
【问题描述】:

我在设置填充或类似于演员的东西时遇到了麻烦。想不通办法。我想我可能必须在皮肤中添加一些东西?

我有这个文本字段:

        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);

看起来像这样:

如您所见,左居中看起来很糟糕......

【问题讨论】:

    标签: android user-interface padding libgdx textfield


    【解决方案1】:

    编辑: 我尝试执行以下操作,并且在我的测试中有效:

    textfieldstyle.background.setLeftWidth(textfieldstyle.background.getLeftWidth() + 10);
    

    搜索 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;
    

    在 draw() 方法中,您可以将填充添加到总和中以定义字符串的新位置。代码在哪里:

    font.draw(batch, displayText, x + bgLeftWidth + textOffset, y + textY + yOffset, visibleTextStart, visibleTextEnd);
    

    替换为:

    font.draw(batch, displayText, x + bgLeftWidth + textOffset + leftPadding, y + textY + yOffset, visibleTextStart, visibleTextEnd);
    

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

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

    mypackage.MyTextField$TextFieldStyle: {
        default: { selection: selection, background: textfield, font: default-font, fontColor: white, cursor: cursor }
    }
    

    并在您的代码中使用它:

    MyTextField txtTest = new MyTextField("Test", skin);
    txtTest.leftPadding = 10.0f;
    

    这不是理想的方式,但会奏效。

    【讨论】:

      【解决方案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();
      

      在主循环中,调用:

      stage.act(Gdx.graphics.getDeltaTime());
      stage.draw();
      

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

      【讨论】:

        【解决方案3】:

        您可以使用 NinePatch。这会将您的纹理分成九个“补丁”,您可以定义它们的高度和宽度,然后将其提供给可以提供给 TextButton 的 NinePatchDrawable。我相信外部“补丁”可以作为边距,通过一些调整(不费力)看起来很棒并实现您的目标。

        我从这篇文章中了解到它们: Loading nine-patch image as a Libgdx Scene2d Button background looks awful

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-23
          • 2013-08-06
          • 2016-05-28
          • 2013-10-04
          • 2014-11-30
          相关资源
          最近更新 更多