ionic3中android状态栏

ionic3状态栏android、ios

android

ionic3中,文档中针对config.xmlStatusBar状态栏的设置只能针对于ios端,android端的设置只能通过其他方式修改,如参考网上基于插件js的修改,并不能满足各个机型的适配。

可通过向 platform/android/app/src/main/java/io/ionic/starter下添加以下StatusBarCompat.java文件

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.view.View;
import android.view.ViewGroup;

public class StatusBarCompat {
private static final int INVALID_VAL = -1;
private static final int COLOR_DEFAULT = Color.parseColor("#20000000");

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void compat(Activity activity, int statusColor) {

//当前手机版本为5.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    if (statusColor != INVALID_VAL) {
        activity.getWindow().setStatusBarColor(statusColor);
        }
    return;
}

//当前手机版本为4.4
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    int color = COLOR_DEFAULT;
    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
    if (statusColor != INVALID_VAL) {
        color = statusColor;
        }
    View statusBarView = new View(activity);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
    getStatusBarHeight(activity));
    statusBarView.setBackgroundColor(color);
    contentView.addView(statusBarView, lp);
    }
}

public static void compat(Activity activity) {
    compat(activity, INVALID_VAL);
}

public static int getStatusBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
    return result;
}

}

然后在MainActivity.java中的onCreate()方法后加入代码

//设置状态栏颜色 #551b1d23 可配置为主题色
StatusBarCompat.compat(this, Color.parseColor("#551b1d23"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
  View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}

这样就可以通过原生的代码简单的修改状态栏颜色。

---------------------------分割线------------------

ios

Q1. 状态栏和app重叠解决方案

A1:在config.xml中加入




or 通过状态栏插件设置

A2:this.statusBar.overlaysWebView(false);



你可能感兴趣的:(ionic3)