android通知栏和标题栏的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:55:31  来源:igfitidea点击:

Size of android notification bar and title bar?

android

提问by lalalei

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:

有没有办法在android中获取通知栏和标题栏的大小?目前我通过以下方式获得显示宽度和高度:

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.

之后我想减去条的大小,以便我可以在不丢失纵横比的情况下拉伸视频。目前我隐藏了栏,因为我看不到更好的方法。

采纳答案by DonGru

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

也许这是一种有用的方法:参考图标设计指南,根据屏幕密度,状态(通知)栏只有三种不同的高度:

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

So if you retrieve the screen density of the device using densityDpiof DisplayMetricsyou know which value to subtract

所以,如果你使用检索设备的屏幕密度densityDpiDisplayMetrics你知道减去其值

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");

回答by sh0m1

*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;
        }*

回答by cdhabecker

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:

Martin 的回答指定了错误的高度(至少从 SDKv11 开始)。根据Icon Design Guidelines,状态栏图标的高度为 25dp,而不是 32dp。25dp 转化为这些特定于密度的高度:

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

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 mTopfor 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).

观察这些大小的一种简单方法是对具有正常密度的模拟器或设备使用层次查看器。只需查看getHeight()标题栏的 FrameLayout 的值。状态栏有点棘手,因为它是 DecorView 的一部分而不是它本身的视图,所以通过查看mTop标题栏的 FrameLayout 来间接获得它的高度,它位于状态栏的正下方。根据我的观察,状态栏和标题栏都与状态栏图标的 25dp 高度相匹配(我没有在 SDKv11 的 android 参考中看到这一事实的声明)。

回答by Jorgesys

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

嗨,我认为这不是必需的,如果您使用 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();         

回答by TheRealChx101

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.

这比硬编码值更有效,因为 android 仍然会返回该特定设备上状态栏或操作栏的高度值,即使它们可能不可见。

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.

上面代码的好处是它还可以考虑操作栏。

回答by Ashwin

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;

回答by Raghav Chopra

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

我找到了一个教程,它可能对你有帮助,对我有帮助! 如何在 Android 应用程序中增加标题栏的大小?你可以改变你的标题栏大小和你想要的方式你可以得到它幸运的家伙!玩得开心

回答by Dvide Leprechaun

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 topof 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.

很抱歉发表虚假评论,但使用常量作为通知栏的高度是不正确的。只需获取全局视图(PhoneWindow$DecorView)然后获取它的子视图(它将只有一个视图(FrameLayout))。此布局包含您的应用程序视图并测量整个屏幕(@see FrameLayout.mHeight)。它的第一个布局将是您的第一个视图,因此获取它的顶部将为您提供正确的通知栏高度。在没有上下文的情况下使用 FrameLayout 不会给您带来任何好处,因为在这种情况下,您孩子的顶部将等于 0。

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

而不是YourActivityViewHere