简体   繁体   English

Libgdx-为滚动窗格创建更大的背景

[英]Libgdx - Creating larger background for scrollpane

I'm trying to create a scrollpane that has a background. 我正在尝试创建一个具有背景的滚动窗格。 However, I want the background to take up more than the area of the scrollpane. 但是,我希望背景占用的空间超过滚动窗格的面积。 For instance, Here is my background and colored in red is where I want the scrollpane to be: 例如,这是我的背景,红色是我想要滚动窗格所在的位置:

滚动窗格

However, when I add buttons I get the following result: 但是,当我添加按钮时,得到以下结果: 在此处输入图片说明

How can I limit the actual scrollpane part to just a section (colored in red in the above picture)? 如何将实际的滚动窗格部分限制为一个部分(上图中以红色显示)?

Here is what I have so far. 这是我到目前为止所拥有的。 I toyed around with spacing/padding but that did not produce any good results: 我开玩笑地用空格/填充,但是没有产生任何好的结果:

Skin skin = Resources.getSkin(Resources.SKIN_JSON);
container = new Table();
container.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
this.addActor(container);
Table table = new Table();
// table.debug();

final ScrollPane scroll = new ScrollPane(table, skin);
table.pad(10).defaults().expandX().space(4);
table.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);

container.add(scroll).expand().fill().colspan(1);
container.row().space(10).padBottom(10);

Image background = new Image(Resources.getAtlas(Resources.SKIN_ATLAS).findRegion("main-panel-horz"));
container.setBackground(background.getDrawable());

TextButton button1 = new TextButton("Button1", skin);
table.add(button1);

table.row();
TextButton button2 = new TextButton("button2", skin);
table.add(button2);

table.row();
TextButton button3 = new TextButton("button3", skin);
table.add(button3);

table.row();
TextButton button4 = new TextButton("button4", skin);
table.add(button4);

What you need to do is pad the outer table to get its contents to fit in the region of the background that you would like. 您需要做的就是填充外部表格,使其内容适合您想要的背景区域。 (Or you could alternatively pad the cell that you put the scroll pane in.) (或者,您也可以填充放入滚动窗格的单元格。)

If you use a 9-patch drawable for the background, the padding of the outer table will be done for you automatically. 如果您使用9色可绘制背景作为背景,则外部表格的填充将自动为您完成。 You could also specify a TextureRegionDrawable in your skin Json and use that as the background. 您还可以在外观Json中指定TextureRegionDrawable并将其用作背景。 That would also allow you to pad the table automatically. 那也将允许您自动填充桌子。

You can easily get texture region drawables from the skin with skin.getDrawable(drawableName); 您可以使用skin.getDrawable(drawableName);从皮肤轻松获取纹理区域可skin.getDrawable(drawableName); --no need to create an intermediate Image just to create a drawable out of a region. -无需创建中间图像,只需创建区域之外的可绘制对象即可。

Here's how you would create a TextureRegionDrawable with explicit padding in json: 这是在json中使用显式填充创建TextureRegionDrawable的方法:

com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable: {
    main-panel-horz-padded: { region: main-panel-horz, leftWidth: 50, rightWidth: 50, topHeight: 50, bottomHeight: 50 }
},

Then in your code: container.setBackground(skin.getDrawable("main-panel-horz-padded")); 然后在您的代码中: container.setBackground(skin.getDrawable("main-panel-horz-padded"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM