ionic1.x 版本的透明狀態列的實現
阿新 • • 發佈:2019-01-31
實現效果:
1.安裝cordova-plugin-statusbar外掛
cordova plugin add https://github.com/apache/cordova-plugin-statusbar.git
2.修改外掛的底層程式碼,進入platforms->android->src->org->apache->cordova->statusbar中找到StatusBar.java檔案。然後修改run方法即可:
將run方法裡面的程式碼替換成以下程式碼,相容版本是android 4.4以上。
// Clear flag FLAG_FORCE_NOT_FULLSCREEN which is set initially
// by the Cordova.
Window window = cordova.getActivity().getWindow();
/**
* remark zqh
* 修改透明狀態列,相容android4.4以上版本
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ){
//新增內容start
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.addFlags(WindowManager.LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}else{
//透明狀態列
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導航欄
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
修改後,執行專案就可以實現透明狀態列了。
補充:以上方式發現在有些機型會出現問虛擬鍵遮擋底部導航欄並與透明狀態列衝突問題,所以想出了以下的方式進行適配。
註釋原來方式的程式碼,在platform/android/src/com/…/MainActivity中加入以下程式碼。
/**
* 判斷底部navigator是否已經顯示
* @return
* @paramwindowManager
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean hasSoftKeys(WindowManager windowManager) {
Display d = windowManager.getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
/**
* 處理虛擬鍵手機遮擋底部導航欄並與透明狀態列衝突問題
*/
public void dealSoftKeys() {
if (hasSoftKeys(getWindowManager())) {
//有虛擬鍵的取消狀態列渲染防止底部導航欄被虛擬鍵遮擋
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//5.0實現全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//5.0設定顏色
getWindow().setStatusBarColor(Color.TRANSPARENT);
getWindow().setNavigationBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//4.4實現全屏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
}
最後在onCreate函式中呼叫 dealSoftKeys();
作者:ZQH24