android 獲取各種窗體高度

2021-09-02 16:21:33 字數 1350 閱讀 5680

一. 獲取螢幕的寬高

//取得視窗屬性

getwindowmanager().getdefaultdisplay().getmetrics(dm);

//視窗的寬度

int screenwidth = dm.widthpix

els;

//視窗高度

int screenheight = dm.heightpixels;

textview = (textview)findviewbyid(r.id.textview01);

textview.settext("螢幕寬度: " + screenwidth + "\n螢幕高度: " + screenheight);

二、獲取狀態列高度

decorview是window中的最頂層view,可以從window中獲取到decorview,然後decorview有個getwindowvisibledisplayframe方法

可以獲取到程式顯示的區域,包括標題欄,但不包括狀態列。 於是,我們就可以算出狀態列的高度了。

rect frame = new rect();

getwindow().getdecorview().getwindowvisibledisplayframe(frame);

int statusbarheight = frame.top;

三、獲取標題欄高度

getwindow().findviewbyid(window.id_android_content)這個方法獲取到的view就是程式不包括標題欄的部分,

然後就可以知道標題欄的高度了。

int contenttop = getwindow().findviewbyid(window.id_android_content).gettop();

//statusbarheight是上面所求的狀態列的高度

int titlebarheight = contenttop - statusbarheight

例子**:

package com.cn.lhq;

import android.graphics.rect;

import android.os.bundle;

import android.util.log;

import android.view.window;

import android.widget.imageview;

public class main extends activity

});log.v("test", "== ok ==");

}private void viewinited()

}<?xml version="1.0" encoding="utf-8"?>

android字型高度,寬度計算方法

一.字型高度計算 經過實踐證明,該方法計算出來的字型高度,是實際高度的2倍 但是繪製字型的時候,並不是從 0,0 開始繪製的,而是從 0,height 開始繪製文字 如果你的文字是要豎直居中繪製的話 canvas.drawtext text,startx,this.getheight 2 getfo...

jquery獲取各種高度

alert window height 瀏覽器時下視窗可視區域高度 alert document height 瀏覽器時下視窗文件的高度 alert document.body height 瀏覽器時下視窗文件body的高度 alert document.body outerheight true ...

Android獲取StatusBar高度的原理

獲取statusbar工具類 常見的獲取狀態列高度的工具類如下 public static int getstatusbarheightcompat context context if result 0 return result 這裡的status bar height是 來的呢?statusb...