Come posso ottenere l'altezza disponibile dello schermo in Android? Ho bisogno dell'altezza meno la barra di stato/barra dei menu o qualsiasi altra decorazione che potrebbe essere sullo schermo e ho bisogno che funzioni per tutti i dispositivi. Inoltre, ho bisogno di sapere questo nella funzione onCreate. So che questa domanda è stata fatta prima, ma ho già provato le loro soluzioni e nessuna di esse funziona. Ecco alcune delle cose che ho provato:Come ottengo l'altezza dello schermo in Android?
Ciò non tiene conto della barra di stato bar/menù:
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Né fa questo:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
Né questo:
Point size = new Point();
getWindowManager().getDefaultDisplay().getRealSize(size);
screenWidth = size.x;
screenHeight = size.y;
Questo non funziona:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
// since SDK_INT = 1;
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
try
{
// used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)
screenWidth = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
}
catch (Exception ignored)
{
// Do nothing
}
try
{
// used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
screenWidth = realSize.x;
screenHeight = realSize.y;
}
catch (Exception ignored)
{
// Do nothing
}
ho poi utilizzato il seguente codice per sottrarre l'altezza della barra di stato e barra menu dall'altezza schermo:
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0)
result = getResources().getDimensionPixelSize(resourceId);
screenHeight -= result;
result = 0;
if (screenHeight >= screenWidth)
resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
else
resourceId = getResources().getIdentifier("navigation_bar_height_landscape", "dimen", "android");
if (resourceId > 0)
result = getResources().getDimensionPixelSize(resourceId);
screenHeight -= result;
Su API 17 dà correttamente calcola l'altezza della barra di stato e di menu barra in verticale ma non in orizzontale. Su API 10, restituisce 0. Ho bisogno di lavorare in posizione ideale su tutti i dispositivi o API minima 10.
Hai provato questo codice: http://stackoverflow.com/a/13026919/611600 – Ali
Sì, l'ho provato. Vedi la mia risposta qui sotto. Grazie –
possibile duplicato di [Ottieni l'altezza dello schermo in Android] (http://stackoverflow.com/questions/17353822/get-the-screen-height-in-android) –