Android如何获取系统高度、标题栏和状态栏高度

Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);///取得整个视图部分,注意,如果你要设置标题样式,这个必须出现在标题样式之后,否则会出错
int top = rect.top;状态栏的高度,所以rect.height,rect.width分别是系统的高度的宽度
View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);///获得根视图
int top2 = v.getTop();///状态栏标题栏的总高度,所以标题栏的高度为top2-top
int width = v.getWidth();///视图的宽度,这个宽度好像总是最大的那个
int height = v.getHeight();视图的高度,不包括状态栏和标题栏

如果只想取得屏幕大小,可以用
Display display = getWindowManager().getDefaultDisplay() ;
display.getWidth();
display.getHeight();

 

 

  代码见

 

@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		Rect frame = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		// 状态栏高度
		int statusBarHeight = frame.top;
		View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
		int contentTop = v.getTop();

		// statusBarHeight是上面所求的状态栏的高度
		int titleBarHeight = contentTop - statusBarHeight;

		textView = (TextView) findViewById(R.id.textView1);
		textView.setText("标题栏的高度" + Integer.toString(titleBarHeight) + "\n"
				+ "标题栏高度" + statusBarHeight + "\n" + "视图的宽度" + v.getWidth()
				+ "\n" + "视图的高度(不包含状态栏和标题栏)" + v.getHeight());

	}


 效果:

   

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值