通过View提供的方法获取高度方式有两种:

1, 当前显示的view中直接获取当前view高宽
2, 通过Activity的getWindow().findViewById(Window.ID_ANDROID_CONTENT)获取系统当前显示的 view根(是一个framelayout对象),android绘制会将要绘制的view放置在framelayout中绘制。

Display对象获取屏幕高宽

获取display对象 Activity中getWindowManager().getDefaultDisplay()
getWidth() 返回显示界面宽度即屏幕宽度
getHeight() 返回显示界面高度即屏幕高度

由display对象设置DisplayMetrics高宽值,通过DisplayMetrics对象获取屏幕高宽,有点多此一举
getWidth() 返回显示界面宽度即屏幕宽度
getHeight() 返回显示界面高度即屏幕高度


常用一些值计算:
屏幕高宽
Canvas对象 、display对象和DisplayMetrics可获取屏幕的高宽
状态栏高度
View的getWindowVisibleDisplayFrame(Rect outRect)附值outRect后,outRect.top()即是状态栏高度
标题高度
View的getWindowVisibleDisplayFrame(Rect outRect1)附值outRect后,outRect.height()-view.getheight()即是标题高度。

1.获取状态栏高度:

  decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。

  于是,我们就可以算出状态栏的高度了。

 Rect frame = new Rect();   getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);   int statusBarHeight = frame.top;

 2.获取标题栏高度:

  getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。

int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();   //statusBarHeight是上面所求的状态栏的高度   int titleBarHeight = contentTop - statusBarHeight


更多相关文章

  1. Android(安卓)Adapter适配器模板(笔记)
  2. Android(安卓)分析内存的使用情况
  3. android ExpandableListView ExpandableListAdapter使用
  4. Android学习笔记(9)——ListView简介
  5. Android从零开始搭建MVVM架构(4)——LiveData
  6. 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreate的问
  7. 安卓开发日记--2017.10.10
  8. 安卓APP开发中的屏幕适配问题如何解决?
  9. [置顶] 安卓实现序列化之Parcelable接口

随机推荐

  1. Android控件-多选按钮、单选按钮
  2. Android中使用jdbc连接Sqllite
  3. android延迟执行任务(刷新按钮旋转)
  4. Android:Eclipse+ADT+Android SDK 搭建安
  5. “刨根问底”之Android(安卓)消息机制
  6. Android ActionBarSherlock的使用
  7. 打开电话Android系统调用
  8. Android兼容性测试
  9. [置顶] Android如何使用Https
  10. React Native调用Android原生方法