获取状态栏、标题栏、屏幕高度

获取屏幕、状态栏、标题栏高度

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

if (hasFocus) {

//屏幕宽高--包含状态栏。注意,华为的手机不包含下面的【HOME键那一栏】,如1920屏幕只有1794

Point point = new Point();

Display disp = this.getWindowManager().getDefaultDisplay();

disp.getSize(point);

//除去状态栏后的高度。从这两个数据可以算出1920*1080手机状态栏高度为75,即【25*3】,这个值一般是固定的!

Rect rect = new Rect();

this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

//除去状态栏和标题栏的高度,这个值默认也状态栏的一样,即也是75

Rect rect2 = new Rect();

this.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect2);

Log.i("bqt", point.y + "---" + rect.height() + "---" + rect2.height());//【奇酷1920-1848-1776】【华为1794-1719-1644】

}

}

获取屏幕宽高等信息方法总结

方法1,废弃了

int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

int screenHeight = getWindowManager().getDefaultDisplay().getHeight();

Log.e("bqt", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); //screenWidth=1080或720; screenHeight=1920或1280

方法2,API13(3.2)之后才能用

Point outSize = new Point();

getWindowManager().getDefaultDisplay().getSize(outSize);

int screenWidth = outSize.x;

int screenHeight = outSize.y;

Log.e("bqt", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); //screenWidth=1080或720; screenHeight=1920或1280

方法3,可以兼容低版本

DisplayMetrics dm = getResources().getDisplayMetrics();

int screenWidth = dm.widthPixels; // 屏幕宽px

int screenHeight = dm.heightPixels; // 屏幕高px

Log.e("bqt", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); //screenWidth=1080; screenHeight=1920

float density = dm.density; // 像素比例:0.75/1.0/1.5/2.0

int densityDPI = dm.densityDpi; // 每寸像素:120/160/240/320

Log.e("bqt" + "  DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI); // density=3.0或2.0; densityDPI=480或320

float xdpi = dm.xdpi;

float ydpi = dm.ydpi;

Log.e("bqt" + "  DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi); //xdpi=160.42105; ydpi=159.89508;

来自为知笔记(Wiz)

时间: 2024-08-02 19:49:00

获取状态栏、标题栏、屏幕高度的相关文章

Android--获取标题栏,状态栏,屏幕高度

获取状态栏高度 Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; 获取actionBar的高宽度 有一个属性: android:layout_width="?attr/actionBarSize" android:layout_height="?attr/actionBarSize&q

js获取网页和屏幕高度

获取浏览器窗口的可视区域高度和宽度 document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.documentElement.clientWidth ==> 可见区域宽度 document.documentElement.clientHeight ==> 可见区域高度 document.body.clientWidth ==> 网页可见区域宽 document

CSS 使用calc()获取当前可视屏幕高度

来自:https://blog.csdn.net/qq_32063079/article/details/89766442 先了解一下CSS3的相对长度单位(参考详细教程) : 相对长度单位指定了一个长度相对于另一个长度的属性.对于不同的设备相对长度更适用. em 它是描述相对于应用在当前元素的字体尺寸,所以它也是相对长度单位.一般浏览器字体大小默认为16px,则2em == 32px:ex 依赖于英文字母小 x 的高度ch 数字 0 的宽度rem 根元素(html)的 font-sizevw

获取像素密度、屏幕高度、状态栏、标题栏,屏幕截图

演示效果 奇酷1080P 华为1080P 华为720P 屏幕像素参数相关信息表格    像素密度  每英寸像素数  分辨率      分辨率别称      默认图标大小 xxhdpi   3        480   1080*1920   1080P    144*144   重点关注 xhdpi    2        320    720*1280     720P      96*96      适配基准 hdpi     1.5      240    480*800      WVG

Android获取状态栏、标题栏、ActionBar以及屏幕的高度

一.屏幕高度和宽度获取方法 int screenWidth,screenHeight; WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); screenWidth = display.getWidth(); screenHeight = display.getHeight(); 另外一种 DisplayMetrics dm = new Disp

获取window状态栏和标题栏的高度

1.获取状态栏高度: decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏. 于是,我们就可以算出状态栏的高度了. [java] view plaincopy Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFra

Android获取状态栏和标题栏的高度

版权声明:本文为博主原创文章,未经博主允许不得转载. 1.获取状态栏高度: decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏. 于是,我们就可以算出状态栏的高度了. [Java] view plain copy Rect frame = new Rect();getWindow().getDecorView()

【转】Andorid获取状态栏高度

在应用开发中,有时我们需要用代码计算布局的高度,可能需要减去状态栏(status bar)的高度.状态栏高度定义在Android系统尺寸资源中status_bar_height,但这并不是公开可直接使用的,例如像通常使用系统资源那样android.R.dimen.status_bar_height.但是系统给我们提供了一个Resource类,通过这个类我们可以获取资源文件.下边是在Activity中获取的方法 public int getStatusBarHeight() { int resul

获取状态栏高度

Android中获取状态栏高度的两种方法: public static int getStatusHeight(Context context) { int statusHeight = 0; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int resourceId = Integer.parseInt(c