如何在cordova-plugin-statusbar插件基礎上增加獲取狀態欄高度功能


  最近做項目,有一個功能需求需要獲取狀態欄高度,但是遍尋各種插件,都沒有此功能,自己又不會寫原生代碼,很無奈。但是在cordova-plugin-statusbar插件git倉庫的issues里,看到有人提出了同樣的需求,並且有人實現了這個功能且發起了pr,只是插件官方並沒有合並到主分支上且release新版本。於是,自己去合並請求的倉庫里找到了相關代碼,加到了自己本地的插件代碼里。目前僅實踐了android,所以先把安卓相關的改動貼出來記錄一下(紅色代碼部分),之后實踐了iOS后再來補上。

  PS:如果已經添加了platform的話,直接修改plugins里面的內容,打包的時候是不會生效的,所以要么去平台編譯后的代碼里添加修改,或者修改后重新添加platform

  • plugins\cordova-plugin-statusbar\www\statusbar.js
var StatusBar = {
    
    height: function (onSuccess, onError) { exec(onSuccess, onError, "StatusBar", "height", []); },

    isVisible: true,

    ……

};

 

  • plugins\cordova-plugin-statusbar\src\android\StatusBar.java
public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
        LOG.v(TAG, "Executing action: " + action);
        final Activity activity = this.cordova.getActivity();
        final Window window = activity.getWindow();

        if ("_ready".equals(action)) {
            boolean statusBarVisible = (window.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible));
            return true;
        }
        
        if ("height".equals(action)) { float statusBarHeight = getStatusBarHeight(); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarHeight)); return true; }
        ……        
}
……

  private float getStatusBarHeight() { float statusBarHeight = 0; int resourceId = cordova.getActivity().getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { float scaleRatio = cordova.getActivity().getResources().getDisplayMetrics().density; statusBarHeight = cordova.getActivity().getResources().getDimension(resourceId) / scaleRatio; } return statusBarHeight; } private void setStatusBarBackgroundColor(final String colorPref) {
        if (Build.VERSION.SDK_INT >= 21) {
            if (colorPref != null && !colorPref.isEmpty()) {
                final Window window = cordova.getActivity().getWindow();
                // Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
                window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                try {
                    // Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
                    window.getClass().getMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
                } catch (IllegalArgumentException ignore) {
                    LOG.e(TAG, "Invalid hexString argument, use f.i. '#999999'");
                } catch (Exception ignore) {
                    // this should not happen, only in case Android removes this method in a version > 21
                    LOG.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
                }
            }
        }
    }

……

 

  • plugins\cordova-plugin-statusbar\src\browser\StatusBarProxy.js
……

module.exports = { isVisible: false, height: 0, styleBlackTranslucent:notSupported, styleDefault:notSupported, styleLightContent:notSupported, styleBlackOpaque:notSupported, overlaysWebView:notSupported, styleLightContect: notSupported, backgroundColorByName: notSupported, backgroundColorByHexString: notSupported, hide: notSupported, show: notSupported, _ready:notSupported };

……

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM