# 如何阻擋android原生的statusBar 因為公司要做自己的Launcher,花了非~~~~長多的時間研究怎麼樣可以阻擋原生的statusBar(就是手機的狀態欄,下拉會有notification與setting開關),查到的方法大部分都是去修改android原生系統的程式碼,但公司的機種會隨著android系統升級,這就代表如果去改原生地程式碼,未來升級時要花更多的心力去更新,因此只能另尋他法。 我最後採用的方法是"使用浮動視窗去遮蓋status bar" 優點: - 方便,不影響原生的程式碼 缺點: - 因為我們開發的是Launcher,如果客戶有自己的需求,需要去開發全螢幕的app,會導致原本是status bar的地方被遮住不能click。 建立一個View後addView到現在的畫面上,isForeground是用來控制是否覆蓋住statusBar的boolean ```java= WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(); localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; localLayoutParams.gravity = Gravity.TOP; localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; if (isForeground) { localLayoutParams.height = getStatusBarHeight(); } else { localLayoutParams.height = getStatusBarHeight() + 100; } Log.d(TAG, String.format("Set StatusBarBlocker as %d", localLayoutParams.height)); localLayoutParams.format = PixelFormat.TRANSPARENT; /*這一行新增自己要的View,我自己新增了一個class叫CustomViewGroup.java*/ CustomViewGroup tmp = new CustomViewGroup(this, manager, adminModeConfig, this,notiListAdapter); manager.addView(tmp, localLayoutParams); ``` 如何拿到原生statusBar的Height: ```java= private int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } ```