首页 > 代码库 > Android如何获取系统高度、标题栏和状态栏高度
Android如何获取系统高度、标题栏和状态栏高度
在android应用中,有时需要计算个View的位置,导致需要计算状态栏高度,标题栏高度等信息。为以后方便,在此做个简单记录。
晒代码前先了解一下android屏幕区域的划分,如下图(该图引用自此文http://www.iteye.com/topic/828830 )
1、 屏幕区域的获取
[java] view plaincopy
- activity.getWindowManager().getDefaultDisplay();
2、应用区域的获取
[java] view plaincopy
- Rect outRect = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
其中,outRect.top 即是状态栏高度。
3、view绘制区域获取
[java] view plaincopy
- Rect outRect = new Rect();
- activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
用绘制区域的outRect.top - 应用区域的outRect.top 即是标题栏的高度。
注意: 如果刚启动Activity时就要计算这些数据,最好在 onWindowFocusChanged 函数中进行, 否则得到的某些数据可能是错误的,比如,应用区域高宽的获取。
详细代码如下:
[java] view plaincopy
- public class ScreenSize extends Activity {
- private TextView mScreenSizeView ;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_screen_size);
- mScreenSizeView = (TextView) findViewById(R.id.screen_size);
- }
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- super.onWindowFocusChanged(hasFocus);
- if(hasFocus){
- System.out.println("second");
- StringBuilder sb = new StringBuilder();
- Dimension dimen1 = getAreaOne(this);
- Dimension dimen2 = getAreaTwo(this);
- Dimension dimen3 = getAreaThree(this);
- sb.append("Area one : \n\tWidth: "+dimen1.mWidth + ";\tHeight: "+dimen1.mHeight);
- sb.append("\nArea two: \n\tWidth: "+dimen2.mWidth + ";\tHeight: "+dimen2.mHeight);
- sb.append("\nArea three: \n\tWidth: "+dimen3.mWidth + ";\tHeight: "+dimen3.mHeight);
- mScreenSizeView.setText(sb.toString());
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_screen_size, menu);
- return true;
- }
- private Dimension getAreaOne(Activity activity){
- Dimension dimen = new Dimension();
- Display disp = activity.getWindowManager().getDefaultDisplay();
- Point outP = new Point();
- disp.getSize(outP);
- dimen.mWidth = outP.x ;
- dimen.mHeight = outP.y;
- return dimen;
- }
- private Dimension getAreaTwo(Activity activity){
- Dimension dimen = new Dimension();
- Rect outRect = new Rect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
- System.out.println("top:"+outRect.top +" ; left: "+outRect.left) ;
- dimen.mWidth = outRect.width() ;
- dimen.mHeight = outRect.height();
- return dimen;
- }
- private Dimension getAreaThree(Activity activity){
- Dimension dimen = new Dimension();
- // 用户绘制区域
- Rect outRect = new Rect();
- activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
- dimen.mWidth = outRect.width() ;
- dimen.mHeight = outRect.height();
- // end
- return dimen;
- }
[java] view plaincopy
- private class Dimension {
- public int mWidth ;
- public int mHeight ;
- public Dimension(){}
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。