开发者

Size of android notification bar and title bar?

Is there a way to obtain the size of the notification bar and title bar开发者_如何学编程 in android? At the moment I obtain the display width and height with:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

After that I want to subtract the sizes of the bars so that I can stretch a video without losing aspect ratio. Currently I hide the bars because I can't see a better way.


Maybe this is a helpful approach: Referring to the Icon Design Guidelines there are only three different heights for the status (notification) bar depending on the screen density:

  • 24px for LDPI
  • 32px for MDPI
  • 48px for HDPI

So if you retrieve the screen density of the device using densityDpi of DisplayMetrics you know which value to subtract

so it could look something like that:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int myHeight = 0;

    switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_HIGH:
            Log.i("display", "high");
            myHeight = display.getHeight() - 48;
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            Log.i("display", "medium/default");
            myHeight = display.getHeight() - 32;
            break;
        case DisplayMetrics.DENSITY_LOW:
            Log.i("display", "low");
            myHeight = display.getHeight() - 24;
            break;
        default:
            Log.i("display", "Unknown density");


*As for as I know, this works perfectly.

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


Martin's answer specified the wrong height (at least as of SDKv11). As per Icon Design Guidelines, the status bar icons have a height of 25dp, not 32dp. 25dp translates into these density-specific heights:

  • 19px for LDPI
  • 25px for MDPI
  • 38px for HDPI
  • 50px for XHDPI

An easy way to observe these sizes is to use the hierarchyviewer against an emulator or device with a normal density. Simply look at the value of getHeight() for the title bar's FrameLayout. The status bar is a bit trickier because it's part of the DecorView and not a view in itself, so get its height indirectly by looking at mTop for the title bar's FrameLayout, which is positioned immediately below the status bar. Per my observations the status bar and title bar each match the 25dp height of the status bar icons (I have not seen a declaration of this fact in the android ref as of SDKv11).


I use the following code for getting heights:

For Status (Notification) bar:

View decorView = getWindow().getDecorView();
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;

For Title bar:

View contentView = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int[] location = new int[2];
contentView.getLocationInWindow(location);
int titleBarHeight = location[1] - statusBarHeight;


hi i think that isn´t necessary and that will be automatically if you use a VideoView

VideoView vv = (VideoView) findViewById(R.id.MainVideo);                        
    MediaController mc=new MediaController(this);
    mc.setEnabled(true);
    mc.show(0);
    vv.setMediaController(mc); 
    vv.setVideoURI(Uri.parse(URLMedia));
    vv.requestFocus();
    vv.showContextMenu();
    vv.start();         


i have found a tutorial may be it would be helpful for u helpful for me thou! How to increase the size of the title bar in an Android application? you can change your title bar size and the way you want it you can get it lucky chap ! have Fun


sorry for dummy comment, but that's no correct to use constants as height of notification bar. just get the global view (PhoneWindow$DecorView) then get it's child (it'll be only one view (FrameLayout)). this layout contain You'r app views and measure whole screen (@see FrameLayout.mHeight). the first layout of it will be You'r first view, so getting the top of it will give You the right notification bar's height. Using FrameLayout without it's context will bring you nothing, cuz the top of you'r child will be equal to 0 in that case.

for lazy one's :

ViewGroup decoreView = (ViewGroup)*YourActivityViewHere*.getRootView();
int barSize = ((ViewGroup)decoreView.getChildAt(0)).getChildAt(0).getTop();

you can also use

getWindow().getDecorView().getRootView()

instead of YourActivityViewHere


This works waaaay better than hardcoding values because android will still return the value that would be the height of the status bar or action bar on that particular device even though they may not be visible.

So, the idea is to get the content view onto which all of your views are added.

public View getContentView(Activity a) {
        int id = a.getResources().getIdentifier("content", "id", "android");
        return a.findViewById(id);
    }

Then, in your activity

View cView = getContentView(this);
cView.post(()->{ 
int offsetY = cView.getTop(); 
// do whatever here.
});

The good thing with the above code is that it'll also account for the action bar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜