方法定义:
private static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
window.setStatusBarColor(statusColor);
ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
ViewCompat.requestApplyInsets(mChildView);
}
}
方法调用:
public void onAttachedToWindow() {
super.onAttachedToWindow();
if("com.android.settings.Settings".equalsIgnoreCase(getClass().getName())){
setStatusBarColor(SettingsActivity.this, android.graphics.Color.parseColor("#3A96FE"));
}
}
注意: setStatusBarColor()
方法在 onCreate()
和 onResume()
中调用都是不起作用的,最好在 onAttachedToWindow()
方法中调用