質問

how can i get the screen height (pixel) without status bar & actionbar Or It will also helpfull if any body tell how get the heights of statusbar and action bar.I already find the screen height but it including statusbar & action bar. I use support library v7 for action bar.I search online but these solution not work for me.

役に立ちましたか?

解決

get statusbar height you can like that:

 Rect rectangle = new Rect();
 Window window = activity.getWindow();
 window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
 int statusBarTop = rectangle.top;
 int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
 int statusbarHeight = Math.abs(statusBarTop - contentViewTop);

and what about action bar, in actionbarsherlock there is constant values for that: abs__action_bar_default_height. Maybe you should try to find something like that in support lib action bar

p.s. Anyway, since you know status bar height, you can get actionbar height by subtraction of activity's main layout screen X value and status bar height value. It will be looks like this:

 mMainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {

            Rect rectangle = new Rect();
            Window window = getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
            int statusBarHeight = rectangle.top;
            int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            int statusbarHeight = Math.abs(statusBarHeight - contentViewTop);

            int[] location = {0,0};
            mMainLayout.getLocationOnScreen(location);

            int actionbarheight = Math.abs(location[0] -statusBarHeight);
            Toast.makeText(MainActivity.this, actionbarheight + "", 1).show();
        }
    });

this was written "on a fly", just to give you an idea how to do that.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top