所以我在手机上运行我的应用程序,因为我一直在向它发展,所以一切都很好。然后我尝试在平板电脑上运行该应用,我的textBoxes
出现在屏幕上完全不同的位置。我正在使用Libgdx框架,这就是这个类的样子:
public class StoneScreen implements Screen {
OrthographicCamera camera;
final TombStone game;
//Textures and art.
public Texture background, sdStone, arrowBack;
public Sprite backgrounds;
//TextField´s stuff
private Stage stage;
private Skin skin;
ImageButton btnArrow;
float screenWidth, screenHeight, stoneHeight, stoneWidth;
//private FitViewport viewport;
public StoneScreen(TombStone gam) {
this.game = gam;
//Set´s the current device´s height and width into a float for easier usage
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
//Set´s the tombstones height and width
stoneHeight = screenHeight * 0.60f;
stoneWidth = screenWidth *0.60f;
//viewport = new FitViewport(camera, screenWidth, screenHeight)
camera = new OrthographicCamera();
camera.setToOrtho(false, screenWidth, screenHeight);
game.assets.load();
loadStandard();
}
public void loadStandard(){
background = game.assets.background;
sdStone = game.assets.sdStone;
//backgrounds = Assets.backgrounds;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Recieves the screen width and height
float gameHeight = Gdx.graphics.getHeight();
float gameWidth = Gdx.graphics.getWidth();
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
Gdx.app.log("X", "FPS:" + Gdx.graphics.getFramesPerSecond());
game.batch.draw(background,0,0, screenWidth, screenHeight);
game.batch.draw(sdStone, screenWidth *0.20f, screenHeight * 0.20f, screenWidth * 0.60f, screenHeight * 0.60f);
//SpriteBatch batcher = (SpriteBatch)stage.getBatch();
game.batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
//Generates the new font
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("NothingYouCouldDoBold.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 30;
BitmapFont textFont = generator.generateFont(parameter);
generator.dispose(); //Avoiding memory leaks.
Preferences prefs = Gdx.app.getPreferences("preferences");
Skin skin = new Skin();
Skin textSkin = new Skin();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
//Sets skin
skin = new Skin(Gdx.files.internal("uiskin.json"));
//Sets a color to the new font
Color fontcolor = Color.BLACK;
//Sets a new font to the textare n the textfield
TextFieldStyle textstyle = new TextFieldStyle();
textstyle.font = textFont;
textstyle.fontColor = fontcolor;
final TextArea textArea = new TextArea(prefs.getString("textArea", "Enter text:"), textstyle);
textArea.setX(420);
textArea.setY(700);
textArea.setWidth(350);
textArea.setHeight(350);
textArea.setMaxLength(50);
final TextField textField = new TextField(prefs.getString("textField", "Enter name:"), textstyle);
textField.setX(420);
textField.setY(800);
textField.setMaxLength(20);
textField.setWidth(350);
textField.setHeight(100);
//String text = Gdx.app.getPreferences("prefs").getString("text", "Default text if missimg");
//TextField textField = new TextField(text, skin);
//Backbutton
ImageButtonStyle styleTwo = new ImageButtonStyle();
TextureRegionDrawable arrowImage = new TextureRegionDrawable(new TextureRegion(new Texture("arrowLeft.png")));
styleTwo.up = skin.newDrawable(skin.newDrawable(arrowImage));
styleTwo.down = skin.newDrawable(skin.newDrawable(arrowImage));
//Back button
btnArrow = new ImageButton(styleTwo);
btnArrow.setSize(150, 150);
btnArrow.setPosition(450, 10);
stage.addActor(textArea);
stage.addActor(textField);
stage.addActor(btnArrow);
//Backbutton takes us back to mainmenu
btnArrow.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MainScreen(game));
//Saves the entered text.
Preferences prefs = Gdx.app.getPreferences("preferences");
prefs.putString("textField", textField.getText());
prefs.putString("textArea", textArea.getText());
prefs.flush();
}
});
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub.
}
}
我真的无法弄清楚如何解决这个问题。我尝试谷歌一些解决方案,但他们没有得到任何帮助。 TextField和TextArea是给我带来麻烦的。