码迷,mamicode.com
首页 > 其他好文 > 详细

如何在cordova-plugin-statusbar插件基础上增加获取状态栏高度功能

时间:2020-05-12 16:48:44      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:uil   新版本   lag   ring   using   efault   webview   height   res   

  最近做项目,有一个功能需求需要获取状态栏高度,但是遍寻各种插件,都没有此功能,自己又不会写原生代码,很无奈。但是在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 };

……

 

如何在cordova-plugin-statusbar插件基础上增加获取状态栏高度功能

标签:uil   新版本   lag   ring   using   efault   webview   height   res   

原文地址:https://www.cnblogs.com/happymental/p/12876891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!