欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 取得状态栏、任务栏高度的小例子

程序员文章站 2023-11-20 18:32:34
复制代码 代码如下:package com.aslibra.test; import android.app.activity; import android.grap...

复制代码 代码如下:

package com.aslibra.test;

import android.app.activity;
import android.graphics.rect;
import android.os.bundle;
import android.util.log;
import android.view.window;
import android.widget.imageview;

public class test extends activity {
  imageview iv;

  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);

    iv = (imageview) this.findviewbyid(r.id.imageview01);
    iv.post(new runnable()
    {
      public void run()
      {
        viewinited();
      }
    }
    );
    log.v("test", "== ok ==");
  }

  private void viewinited(){
    rect rect= new rect();
    window window= getwindow();
    iv.getwindowvisibledisplayframe(rect);
    int statusbarheight= rect.top;
    int contentviewtop= window.findviewbyid(window.id_android_content).gettop();
    int titlebarheight= contentviewtop - statusbarheight;
    //测试结果:ok之后 100多 ms 才运行了
    log.v("test", "=-init-= statusbarheight="+statusbarheight+
        " contentviewtop="+contentviewtop+
        " titlebarheight="+titlebarheight);   
  }

  /*
   * 07-14 02:36:48.449: verbose/test(1385): == ok ==
   * 07-14 02:36:48.558: verbose/test(1385): =-init-= statusbarheight=25 contentviewtop=50 titlebarheight=25
   */
}

浏览器获取状态栏高度的代码

复制代码 代码如下:

    rect visrect = new rect();
    if (!mbrowserframelayout.getglobalvisiblerect(visrect)) {
        if (logd_enabled) {
            log.d(logtag, "showfaketitlebar visrect failed");
        }
        return;
    }
    params.y = visrect.top;