Android 获取没有标题/通知栏的窗口大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2485697/
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 06:02:28  来源:igfitidea点击:

Getting the size of the window WITHOUT title/notification bars

androiduser-interface

提问by Victor

I've been playing around with Android development and one of the things I'd like to be able to do is dynamically create a background image for my windows, similar to the one below.

我一直在玩 Android 开发,我希望能够做的一件事是为我的窗口动态创建背景图像,类似于下面的图像。

alt text

替代文字

This is from my BlackBerry app. It consists of three separate parts, the bottom right logo, the top left watermark, and the bottom right name. It works independent of screen size because the BlackBerry app just gets all three parts and generates an appropriately sized bitmap using the screen width and height.

这是来自我的黑莓应用程序。它由三个独立的部分组成,右下角的标志、左上角的水印和右下角的名称。它的工作与屏幕大小无关,因为 BlackBerry 应用程序只获取所有三个部分,并使用屏幕宽度和高度生成适当大小的位图。

Since Android has quite a bit more screen resolution possibilities I need to be able to generate backgrounds on the fly like this. However, I have not found any way to get the height/width of the window in Android. I can get the screen resolution, but that includes the application title bar and the notification bar, which is unacceptable.

由于 Android 有更多的屏幕分辨率可能性,我需要能够像这样即时生成背景。但是,我还没有找到任何方法来获取 Android 中窗口的高度/宽度。我可以获得屏幕分辨率,但这包括应用程序标题栏和通知栏,这是不可接受的。

I'd like to know how to get the size of my window, or screen resolution minus the title and notification bars. I think this might be possible using the dimensions of my layout managers but I cannot get the height/width of them in the onCreate method so I'm not sure what to do exactly.

我想知道如何获得我的窗口大小或屏幕分辨率减去标题和通知栏。我认为这可能使用我的布局管理器的尺寸,但我无法在 onCreate 方法中获得它们的高度/宽度,所以我不确定该怎么做。

Thanks.

谢谢。

采纳答案by Evgeny

Use View.MeasureSpec.getSizemethod in onMeasureoverride.

onMeasure覆盖中使用View.MeasureSpec.getSize方法。

    @Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    ...
}

回答by Evgeni Shafran

I have a tutorial on my blog that give you the ability to save your screen size on launch, You can read it here: http://evgeni-shafran.blogspot.com/2011/01/android-screen-size-problem.html

我的博客上有一个教程,可以让您在启动时保存屏幕尺寸,您可以在这里阅读:http: //evgeni-shafran.blogspot.com/2011/01/android-screen-size-problem。 html

Basicly you need to overide an onMeasure method of the first layout that hold you full screen, and get the width and height from there

基本上你需要覆盖第一个布局的 onMeasure 方法,让你全屏,并从那里获取宽度和高度

回答by Sephy

have a look at getwidth() and getheight() maybe? that should give you the size of the screen. though, I don't know If it takes the bars or not. But I don't think so...

看看 getwidth() 和 getheight() 可能吗?这应该给你屏幕的大小。不过,我不知道是否需要酒吧。但我不这么认为...

回答by SteelBytes

in the first onDraw, call getWidth, getHeight. these will not be valid before then.

在第一个 onDraw 中,调用 getWidth、getHeight。在此之前这些将无效。

if you are not using a custom view/layout then you can instead call getWidth/getHeight during the first onWindowFocusChanged

如果您没有使用自定义视图/布局,那么您可以在第一次 onWindowFocusChanged 期间调用 getWidth/getHeight

回答by rat

To accomplish this in my app, I had to find a View's dimensions within the main Activity. It looked something like this:

为了在我的应用程序中实现这一点,我必须在主活动中找到一个视图的维度。它看起来像这样:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    gestureScanner = new GestureDetector(this);   //Object for handling gestures.

    mvView = new MyView(this);  //MyView is class that extends View.
    setContentView(myView);
}

@Override
public void onShowPress(MotionEvent e) {

    //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();


    Toast toast = Toast.makeText(this," View:" + iViewWidth + ","+iViewHeight + " Window: " + dm.widthPixels + " by " + dm.heightPixels, 2);
    toast.show();
}