最近、libgdxを使用してAndroidプロジェクトを開発しています。期間中に質問が発生しました。ソフト キーボードが表示されると、一部のビューが覆われるため、このバグを解決するための高さを取得したいと考えています。
Android APIを使用してプロジェクトを開発するときに、この問題を解決するためにソフト入力モードを設定できることを知っています.libgdxは何か方法を提供しますか?
最近、libgdxを使用してAndroidプロジェクトを開発しています。期間中に質問が発生しました。ソフト キーボードが表示されると、一部のビューが覆われるため、このバグを解決するための高さを取得したいと考えています。
Android APIを使用してプロジェクトを開発するときに、この問題を解決するためにソフト入力モードを設定できることを知っています.libgdxは何か方法を提供しますか?
共有したい実用的なソリューションがあります。
まず、libgdx API からソフト キーボードの高さを取得する方法がありません。プラットフォーム固有のコードを作成する必要があります。プラットフォーム固有のコードとのインターフェースはかなり単純です - 心配しないでください! 公式の libgdx wiki からこのガイドを読んでください。
次のコードはネイティブ Android コードであり、libgdx Android gradle モジュールに配置する必要があります。
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import com.badlogic.gdx.backends.android.AndroidApplication;
/**
* Container for platform-specific android implementation.
*/
public class PlatformSpecificAndroidImpl implements PlatformSpecificService {
private AndroidApplication androidApplication;
private AndroidGlobalLayoutListener globalLayoutListener;
public PlatformSpecificAndroidImpl(AndroidApplication androidApplication) {
this.androidApplication = androidApplication;
}
/**
* Initialize platform services. This method should be called from the gdx applications "create()" method.
*/
@Override
public void init() {
globalLayoutListener = new AndroidGlobalLayoutListener(androidApplication);
Window window = androidApplication.getWindow();
if (window != null) {
View decorView = window.getDecorView();
if (decorView != null) {
View rootView = decorView.getRootView();
if (rootView != null) {
ViewTreeObserver viewTreeObserver= rootView.getViewTreeObserver();
if (viewTreeObserver != null) {
viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);
}
}
}
}
}
/**
* Get the window height that is really usable, subtracting the soft-keyboard if open.
* @return usable window height
*/
@Override
public int getUsableWindowHeight() {
if (globalLayoutListener != null) {
return globalLayoutListener.getHeight();
}
return 0;
}
private static class AndroidGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private AndroidApplication androidApplication;
private int height;
private AndroidGlobalLayoutListener(AndroidApplication androidApplication) {
this.androidApplication = androidApplication;
}
@Override
public void onGlobalLayout() {
height = 0;
Window window = androidApplication.getWindow();
if (window != null) {
View currentFocus = window.getCurrentFocus();
if (currentFocus != null) {
View rootView = currentFocus.getRootView();
if (rootView != null) {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
height = rect.bottom;
}
}
}
}
public int getHeight() {
return height;
}
}
}
libgdx コア モジュール内getUsableWindowHeight()
から、PlatformSpecificService インターフェイスを介してメソッドを呼び出すことができるようになりました。ディスプレイの高さ (ピクセル単位) からソフト キーボードの高さを引いた値を返します。
OnGlobalLayoutListener を使用し、要求されたときに getter メソッドで高さを直接計算しなかったのはなぜですか? まあ、私のオプションでは、この方法はもう少しエレガントなソリューションです。
注意すべき問題がもう 1 つあります。ソフト キーボードを開くには、Gdx.input.setOnscreenKeyboardVisible(true);
非同期通信が必要です。setOnscreenKeyboardVisible の直後にたまたま呼び出した場合getUsableWindowHeight()
でも、キーボードがまだ実際に開いていないため、ディスプレイの高さ全体が得られます。
はい、Viewtree Observer とグローバル レイアウト リスナーの助けを借りて、以下の手順を試してください。
これで、ソフト キーボードが表示されるたびに、Android が画面のサイズを変更し、リスナーに電話がかかります。サイズ変更後のルートビューの高さと元のサイズの差を計算するだけです。差が 150 を超える場合は、キーボードが膨らんでいると考えてください。
以下はサンプルコードです
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout(){
int heightDiff = root.getRootView().getHeight()- root.getHeight();
// IF height diff is more then 150, consider keyboard as visible.
}
});