如何在像素中获取Android屏幕高度减去状态栏?

时间:2013-05-12 18:45:35

标签: android

我正在使用这个厚,但它返回“0”

    View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
    int viewHeight = content.getHeight();

怎么做?

3 个答案:

答案 0 :(得分:3)

root = (ViewGroup)findViewById(R.id.root);
root.post(new Runnable() { 
public void run(){
        Rect rect = new Rect();
        Window win = getWindow();
        win.getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusHeight = rect.top;
        int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
        int  titleHeight = contentViewTop - statusHeight;
        Log.w("dimens_tag", "title = " + titleHeight + " status bar = " + statusHeight);
}
});

答案 1 :(得分:0)

根据Dory的回答我接下来探讨了:

            Window win = getWindow();
            topBarHeight = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();;
            Rect rect = new Rect();
            win.getDecorView().getWindowVisibleDisplayFrame(rect);
            if (rect.top==0){
                //pad
                int resourceId = getResources().getIdentifier("status_bar_height","dimen", "android");
                if (resourceId > 0)
                    bottomBarHeight=getResources().getDimensionPixelSize(resourceId);
            } else
                bottomBarHeight=0;

答案 2 :(得分:-2)

//Returns the size of the entire window, including status bar and title.
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);

//Try to find the dimension of the view without the status/title bars.
int iViewHeight = mvMountain.getHeight();
int iViewWidth = mvMountain.getWidth();