Pertanyaan Mendapatkan tinggi STATUS BAR


Saya sedang menulis private untuk mendapatkan status bar's tinggi.

private int statusBar() {
    Rect rect = new Rect();
    int statusBar = rect.top;
    return statusBar;
}

Semuanya baik sampai sekarang. tetapi ketika saya melakukannya:

Toast.makeText(getApplicationContext(), "" + statusBar(), Toast.LENGTH_LONG).show();

Ia mengembalikan 0.


4
2017-11-29 08:31


asal


Jawaban:


Baru saja mencari situsnya dan mendapatkan ini.

private int statusBar() {
    Rect rectgle= new Rect();
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int StatusBarHeight= rectgle.top;
    int contentViewTop= 
        window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int statusBar = contentViewTop - StatusBarHeight;
    return statusBar;
}

Roti panggang:

Toast.makeText(getApplicationContext(), "" + statusBar(), Toast.LENGTH_LONG).show();

Sepertinya berhasil.

memperbarui:

coba ini, jika tidak berhasil coba yang lain.

private int statusBar() {
    Rect rectgle= new Rect();
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int statusBar = rectgle.top;
    return statusBar;
}

2

private int statusBar() {
    Rect rectgle= new Rect();
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int StatusBarHeight= rectgle.top;
    int contentViewTop= 
        window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int statusBar = contentViewTop + StatusBarHeight;
    return statusBar;
}

2
2017-11-29 08:34



Coba ini:

Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;

Kemudian Toast  statusBarHeight


0
2017-11-29 09:15