下面列出了android.view.Window#getNavigationBarColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setNavigationBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mSettings.getCurrent().mUseMatchingNavbarColor) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final int keyboardColor = Settings.readKeyboardColor(prefs, this);
final Window window = getWindow().getWindow();
if (window == null) {
return;
}
mOriginalNavBarColor = window.getNavigationBarColor();
window.setNavigationBarColor(keyboardColor);
final View view = window.getDecorView();
mOriginalNavBarFlags = view.getSystemUiVisibility();
if (ResourceUtils.isBrightColor(keyboardColor)) {
view.setSystemUiVisibility(mOriginalNavBarFlags | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
} else {
view.setSystemUiVisibility(mOriginalNavBarFlags & ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
}
}
/**
* @see android.view.Window#setStatusBarColor(int color).
*/
public static void setStatusBarColor(Window window, int statusBarColor) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
// If both system bars are black, we can remove these from our layout,
// removing or shrinking the SurfaceFlinger overlay required for our views.
// This benefits battery usage on L and M. However, this no longer provides a battery
// benefit as of N and starts to cause flicker bugs on O, so don't bother on O and up.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && statusBarColor == Color.BLACK
&& window.getNavigationBarColor() == Color.BLACK) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setStatusBarColor(statusBarColor);
}
private void setNavigationBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mSettings.getCurrent().mUseMatchingNavbarColor) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final int keyboardColor = Settings.readKeyboardColor(prefs, this);
final Window window = getWindow().getWindow();
if (window == null) {
return;
}
mOriginalNavBarColor = window.getNavigationBarColor();
window.setNavigationBarColor(keyboardColor);
final View view = window.getDecorView();
mOriginalNavBarFlags = view.getSystemUiVisibility();
if (ResourceUtils.isBrightColor(keyboardColor)) {
view.setSystemUiVisibility(mOriginalNavBarFlags | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
} else {
view.setSystemUiVisibility(mOriginalNavBarFlags & ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
}
}
public static ActivityStatusRecord newInstance(Activity activity) {
ActivityStatusRecord record = new ActivityStatusRecord();
Window window = activity.getWindow();
View decorView = window.getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
record.mStatusBarColor = window.getStatusBarColor();
record.mNavigationBarColor = window.getNavigationBarColor();
}
record.mSystemUiVisibility = decorView.getSystemUiVisibility();
record.mSoftInputMode = window.getAttributes().softInputMode;
record.mWindowFlags = window.getAttributes().flags;
record.mRequestedOrientation = activity.getRequestedOrientation();
return record;
}
/**
* 获取 Navigation Bar 颜色
* @param window {@link Window}
* @return Navigation Bar 颜色
*/
public static int getNavBarColor(final Window window) {
if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return window.getNavigationBarColor();
}
return -1;
}
/**
* @see android.view.Window#setStatusBarColor(int color).
*/
public static void setStatusBarColor(Window window, int statusBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// If both system bars are black, we can remove these from our layout,
// removing or shrinking the SurfaceFlinger overlay required for our views.
if (statusBarColor == Color.BLACK && window.getNavigationBarColor() == Color.BLACK) {
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
window.setStatusBarColor(statusBarColor);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NavBarObserver(Activity activity, TypedArray a, int attr) {
Window window = activity.getWindow();
mNavigationBarColor = window.getNavigationBarColor();
mNavigationBarColorNight = a.getColor(attr,mNavigationBarColor);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ExpandedScreen(Activity activity, boolean enabled) {
this.activity = activity;
statusBar = new StatusBarController(activity);
navigationBar = new NavigationBarController();
Resources resources = activity.getResources();
Window window = activity.getWindow();
boolean fullScreenLayoutEnabled;
if (C.API_LOLLIPOP) {
fullScreenLayoutEnabled = true;
} else if (C.API_KITKAT) {
int resId = resources.getIdentifier("config_enableTranslucentDecor", "bool", "android");
fullScreenLayoutEnabled = resId != 0 && resources.getBoolean(resId);
} else {
fullScreenLayoutEnabled = false;
}
expandingEnabled = enabled;
this.fullScreenLayoutEnabled = fullScreenLayoutEnabled;
float density = ResourceUtils.obtainDensity(resources);
slopShiftSize = (int) (6f * density);
lastItemLimit = (int) (72f * density);
if (fullScreenLayoutEnabled) {
if (C.API_LOLLIPOP) {
int statusBarColor = window.getStatusBarColor() | Color.BLACK;
int navigationBarColor = window.getNavigationBarColor() | Color.BLACK;
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
contentForeground = new LollipopContentForeground(statusBarColor, navigationBarColor);
statusBarContentForeground = new LollipopStatusBarForeground(statusBarColor);
statusBarDrawerForeground = new LollipopDrawerForeground();
} else {
contentForeground = new KitKatContentForeground();
statusBarContentForeground = null;
statusBarDrawerForeground = null;
}
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
} else {
contentForeground = null;
statusBarContentForeground = null;
statusBarDrawerForeground = null;
if (enabled) {
window.requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
}
}
readConfiguration(resources.getConfiguration());
}
/**
* Return the color of navigation bar.
*
* @param window The window.
* @return the color of navigation bar
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public static int getNavBarColor(@NonNull final Window window) {
return window.getNavigationBarColor();
}
/**
* Return the color of navigation bar.
*
* @param window The window.
* @return the color of navigation bar
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public static int getNavBarColor(@NonNull final Window window) {
return window.getNavigationBarColor();
}