我有一个奇怪的问题,没有在Android设备上显示的按钮。让我向您介绍构建按钮的代码:
private void setUpRetry() {
texture = new TextureAtlas("ui/button.pack");
skin = new Skin(texture);
BitmapFont white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("red_button01");
textButtonStyle.down = skin.getDrawable("red_button02");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
buttonRetry = new TextButton("Retry", textButtonStyle);
buttonRetry.addListener(new ClickListener() {
public void clicked(InputEvent evet, float x, float y) {
((Game) Gdx.app.getApplicationListener()).setScreen(new GameScreen());
}
});
buttonRetry.pad(40, 150, 40, 150);
buttonRetry.setPosition(Gdx.graphics.getWidth() / 2 - buttonRetry.getWidth() / 2,
Gdx.graphics.getHeight() - buttonRetry.getHeight()); // this don't
buttonRetry.setPosition(305, 430); // this line works
this.addActor(buttonRetry);
}
我想确保将来参考我不会遇到屏幕中间的按钮问题。因此,我想设置按钮而不是我的坐标,而是设置应用程序的原生宽度和高度。然而,我似乎无法完成它。 问题是设定高度。如果我用400替换它 - 按钮显示!
如果有帮助的话,我正在三星Galaxy S5上进行调试。
如果我/我们无法解决问题,是否可以按数字设置按钮的坐标,而不是原生宽度等等?
这两行的桌面版图片已注释掉:
如您所见,桌面将它们放在同一位置。
编辑:
我已经使用了可行的方法,但由于问题尚未解决,因此会将问题保持打开状态。
答案 0 :(得分:0)
这个答案可能包含一些旧信息。
在某些环境中,显示分辨率可能会导致定位问题。 要解决问题,缩放职位。
下面的示例/测试代码(Game.java)
onWindowResize()
:未缩放
onWindowResize_Scaled()
:缩放
(旧维基) https://code.google.com/p/libgdx-users/wiki/DisplayResolution
(伪造桌面DPI) http://badlogicgames.com/forum/viewtopic.php?f=11&t=949
Android libgdx big screen resolution
// MainActivity.java
import android.os.Bundle;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.android.AndroidApplication;
public class MainActivity extends AndroidApplication
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
initialize((ApplicationListener) new Game());
}
}
// Game.java
import android.util.Log;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class Game implements ApplicationListener
{
private int windowWidth;
private int windowHeight;
private float ratioX = 1f;
private float ratioY = 1f;
private TextureAtlas texture;
private Skin skin;
private Table table;
private TextButton buttonRetry;
@Override
public void create()
{
setUpRetry();
}
@Override
public void dispose()
{
}
@Override
public void pause()
{
}
@Override
public void render()
{
}
@Override
public void resize(int arg0, int arg1)
{
if ((0 == arg0) || (0 == arg1))
{
// may not occur
return;
}
this.windowWidth = arg0; // If display resolution is hard coded, this may not match maGdx.graphics.getWidth()
this.windowHeight = arg1;
Log.d("resize", "arg:(" + windowWidth + ", " + windowHeight + ")"); // just for check
// setUpRetry();
// onWindowResize();
onWindowResize_Scaled(); // In dpi (or fixed resolution) problem, use this.
}
@Override
public void resume()
{
}
private void onWindowResize()
{
//
// Fixed ratio
// Use (arg0, arg1) in resize() as (windowWidth, windowHeight)
//
if ((0 == windowWidth) || (0 == windowHeight))
{
// called before resize()
return;
}
// ratioX = 1f;
// ratioY = 1f;
if (null != table)
{
table.setBounds(0, 0, windowWidth, windowHeight);
}
if (null != buttonRetry)
{
buttonRetry.pad(40, 150, 40, 150); // This may be effected by dpi, but it's hard coded. Just adjust (or use ratioX and ratioY to scale).
{
// If position has minus value (may be called before resize()), view will be displayed at unwanted position.
float posX = (windowWidth - buttonRetry.getWidth()) / 2;
float posY = (windowHeight - buttonRetry.getHeight());
buttonRetry.setPosition(posX, posY);
}
// buttonRetry.setPosition(305, 430); // this line works
Log.d("onWindowResize", "pos:(" + buttonRetry.getX() + ", " + buttonRetry.getY() + ")"); // for check
}
}
private void onWindowResize_Scaled()
{
//
// Scaled
// Use (getWidth() * ratioX) and (getHeight() *ratioY) for each view
//
// * This part may contain some old information (as https://code.google.com/p/libgdx-users/wiki/ says the wiki is outdated) *
// If dpi causes unwanted behavior (mentioned in http://badlogicgames.com/forum/viewtopic.php?f=11&t=949)
// use ratioX and ratioY as Gdx size (getWidth() or getHeight()) to displayed size ratio.
// (All window/views are scaled same, so this is not needed I think.)
//
// May be effected by hard coded display resolution (https://code.google.com/p/libgdx-users/wiki/DisplayResolution) or
// View port (https://github.com/libgdx/libgdx/wiki/Viewports)
//
// (from http://badlogicgames.com/forum/viewtopic.php?f=11&t=949)
int windowWidth = Gdx.graphics.getWidth();
int windowHeight = Gdx.graphics.getHeight();
if (Gdx.app.getType() == ApplicationType.Android)
{
ratioX = Gdx.graphics.getPpiX() / 160;
ratioY = Gdx.graphics.getPpiY() / 160;
// ratioX = 160 / Gdx.graphics.getPpiX();
// ratioY = 160 / Gdx.graphics.getPpiY();
Log.d("onWindowResize", "Gdx window Size:(" + windowWidth + ", " + windowHeight + ")"); // just for check
Log.d("onWindowResize", "Scaled window Size:(" + windowWidth * ratioX + ", " + windowHeight * ratioY + ")"); // just for check
Log.d("onWindowResize", "ratio:(" + ratioX + ", " + ratioY + ")"); // just for check
}
else
{
// hard coded resolution (480 x 320) for desktop (if not changed from default)
// https://code.google.com/p/libgdx-users/wiki/DisplayResolution
int w;
int h;
boolean isLandscape = windowWidth > windowHeight;
if (isLandscape)
{
w = 480;
h = 320;
}
else
{
w = 320;
h = 480;
}
ratioX = windowWidth / w;
ratioY = windowHeight / h;
// ratioX = w / windowWidth;
// ratioY = h / windowHeight;
Log.d("onWindowResize", "Gdx window Size:(" + windowWidth + ", " + windowHeight + ")"); // just for check
Log.d("onWindowResize", "Scaled window Size:(" + windowWidth * ratioX + ", " + windowHeight * ratioY + ")"); // just for check
Log.d("onWindowResize", "ratio:(" + ratioX + ", " + ratioY + ")"); // just for check
}
if (null != table)
{
table.setBounds(0, 0, windowWidth * ratioX, windowHeight * ratioY);
}
if (null != buttonRetry)
{
buttonRetry.pad(40, 150, 40, 150); // This may be effected by dpi, but it's hard coded. Just adjust (or use ratioX and ratioY to scale).
{
// If position has minus value (may be called before resize()), view will be displayed at unwanted position.
float posX = (windowWidth - buttonRetry.getWidth()) / 2 * ratioX;
float posY = (windowHeight - buttonRetry.getHeight()) * ratioY;
buttonRetry.setPosition(posX, posY);
}
// buttonRetry.setPosition(305, 430); // this line works
Log.d("onWindowResize", "pos:(" + buttonRetry.getX() + ", " + buttonRetry.getY() + ")"); // for check
}
}
private void setUpRetry()
{
// if ((0 == windowWidth) || (0 == windowHeight))
// {
// // called before resize()
// // Show error message or do some recovery or silently return
// return;
// }
texture = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
skin = new Skin(texture);
BitmapFont white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
table = new Table(skin);
// table.setBounds(0, 0, windowWidth, windowHeight);
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("red_button01");
textButtonStyle.down = skin.getDrawable("red_button02");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
buttonRetry = new TextButton("Retry", textButtonStyle);
buttonRetry.addListener(new ClickListener()
{
public void clicked(InputEvent evet, float x, float y)
{
Game game = (Game) Gdx.app.getApplicationListener();
if ((0 == game.windowWidth) || (0 == game.windowHeight))
{
// called before resize()
// Show error message or do some recovery or silently return
return;
}
game.setScreen(new GameScreen());
}
});
// buttonRetry.pad(40, 150, 40, 150);
// buttonRetry.setPosition((windowWidth - buttonRetry.getWidth()) / 2, windowHeight - buttonRetry.getHeight());
// // buttonRetry.setPosition(305, 430); // this line works
// Log.d("setUpRetry", "pos:(" + buttonRetry.getX() + ", " + buttonRetry.getY() + ")"); // for check
this.addActor(buttonRetry); // If this needs position or size of buttonRetry, (remove old actor and) call in onWindowResize
}
private void setScreen(GameScreen gameScreen)
{
}
private void addActor(TextButton button)
{
}
}
class GameScreen
{
}