(4.1.39)Android屏幕测量:屏幕、状态栏、标题栏

来源:互联网 发布:core keygen for mac 编辑:程序博客网 时间:2024/06/08 11:37

一、基本区域划分

这里写图片描述

1.1 屏幕区域

以下两种方法在屏幕未显示的时候,还是处于0的状态,即要在setContentView调用之后才有效

WindowManager windowManager = getWindowManager();  Display display = windowManager.getDefaultDisplay();  screenWidth = display.getWidth();  screenHeight = display.getHeight();  
DisplayMetrics dm = new DisplayMetrics();  this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity  screenWidth =dm.widthPixels;  screenHeight =dm.heightPixels;  

1.2 应用区域的获取

Rect outRect = new Rect();  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);  

我们可以注意到,应用区域的顶部其实就是状态栏的高度,由此可以得到状态栏高度获取方式:

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

1.3 view绘制区域获取

Rect outRect = new Rect();  activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);  

我们可以注意到“绘制区域的outRect.top - 应用区域的outRect.top ”其实就是标题栏的高度:

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

二、屏幕模式

  • 无标题
 requestWindowFeature(Window.FEATURE_NO_TITLE);    
  • 全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);    
  • 横屏
setRequesteOrientation(ActivityInfo.SCREEN_ORIENTATION_LADSCAPE);   

2.1 全屏无标题示例

用代码方式在我们应用运行后,会看到短暂的状态栏,然后才全屏,而xml配置方法是不会有这种情况的

一、在代码中设置:

 public class OpenGl_Lesson1 extends Activity {         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);            //去除title             requestWindowFeature(Window.FEATURE_NO_TITLE);              //去掉Activity上面的状态栏          getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,                               WindowManager.LayoutParams. FLAG_FULLSCREEN);              setContentView(R.layout.main);         }     }  

在这里要强调一点,设置全屏的俩段代码必须在setContentView(R.layout.main) 之前,不然会报错

二、在配置文件里修改

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"