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

时间:2010-03-21 02:47:59

标签: android user-interface

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

alt text http://i43.tinypic.com/2s1acrb.png

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

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

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

感谢。

5 个答案:

答案 0 :(得分:18)

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

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

答案 1 :(得分:1)

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

基本上你需要覆盖第一个布局的onMeasure方法,它可以全屏显示,并从那里获得宽度和高度

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

如果您没有使用自定义视图/布局,那么您可以在第一个onWindowFocusChanged

期间调用getWidth / getHeight

答案 4 :(得分:-1)

要在我的应用中完成此操作,我必须在主Activity中找到View的尺寸。它看起来像这样:

@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();
}