Android屏幕參數測量:屏幕、狀態欄、標題欄。
一、基本區域劃分
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"