fix: navigation bar height
This commit is contained in:
parent
ceb42af63d
commit
94632a47ee
1 changed files with 53 additions and 2 deletions
|
@ -1,9 +1,15 @@
|
|||
package mw.gri.android;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Point;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.system.ErrnoException;
|
||||
import android.system.Os;
|
||||
import androidx.core.view.WindowCompat;
|
||||
import android.view.Display;
|
||||
import android.view.WindowInsets;
|
||||
import android.view.WindowManager;
|
||||
import com.google.androidgamesdk.GameActivity;
|
||||
|
||||
public class MainActivity extends GameActivity {
|
||||
|
@ -19,7 +25,52 @@ public class MainActivity extends GameActivity {
|
|||
} catch (ErrnoException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
WindowCompat.setDecorFitsSystemWindows(getWindow(), true);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);
|
||||
findViewById(android.R.id.content).setPadding(0, 0, 0, getNavigationBarHeight());
|
||||
}
|
||||
|
||||
public int getNavigationBarHeight() {
|
||||
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= 30) {
|
||||
return windowManager
|
||||
.getCurrentWindowMetrics()
|
||||
.getWindowInsets()
|
||||
.getInsets(WindowInsets.Type.navigationBars())
|
||||
.bottom;
|
||||
} else {
|
||||
Point appUsableSize = getAppUsableScreenSize(this);
|
||||
Point realScreenSize = getRealScreenSize(this);
|
||||
|
||||
// navigation bar on the side
|
||||
if (appUsableSize.x < realScreenSize.x) {
|
||||
return appUsableSize.y;
|
||||
}
|
||||
|
||||
// navigation bar at the bottom
|
||||
if (appUsableSize.y < realScreenSize.y) {
|
||||
return realScreenSize.y - appUsableSize.y;
|
||||
}
|
||||
|
||||
// navigation bar is not present
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public Point getAppUsableScreenSize(Context context) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Point size = new Point();
|
||||
windowManager.getDefaultDisplay().getSize(size);
|
||||
return size;
|
||||
}
|
||||
|
||||
public Point getRealScreenSize(Context context) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = windowManager.getDefaultDisplay();
|
||||
Point size = new Point();
|
||||
display.getRealSize(size);
|
||||
return size;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue