Height of status bar in Android

1. 需要在onCreate的地方處理一些額外步驟

Rect rectangle=newRect();Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);int statusBarHeight= rectangle.top;int contentViewTop= 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();int titleBarHeight= contentViewTop - statusBarHeight;Log.i("*** Jorgesys :: ","StatusBar Height= "+ statusBarHeight +" , TitleBar Height = "+ titleBarHeight);

to get the Height of the status bar on the onCreate() method of your Activity, use this method:

publicint getStatusBarHeight(){int result =0;int resourceId = getResources().getIdentifier("status_bar_height","dimen","android");if(resourceId >0){
          result = getResources().getDimensionPixelSize(resourceId);}return result;}

2.

On MDPI devices, the status bar is 25px. We can use this as the base and multiply it by the density (rounded up) to get the status bar height on any device:

int statusBarHeight =Math.ceil(25* context.getResources().getDisplayMetrics().density);

For reference: ldpi=.75, mdpi=1, hdpi=1.5, xhdpi=2



arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()