下面列出了android.view.Window#setNavigationBarColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int getLayoutId()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
return R.layout.activity_chat_home;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.OSTheme);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View view = localInflater.inflate(R.layout.fragment_os, container, false);
unbinder = ButterKnife.bind(this, view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.dark_blue));
window.setNavigationBarColor(getResources().getColor(R.color.dark_blue));
}
/*
View view = inflater.inflate(R.layout.fragment_os, container, false);
unbinder = ButterKnife.bind(this, view);
*/
return view;
}
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);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//实现半透明状态栏
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity_image_view_pager);
mImageList = getIntent().getParcelableArrayListExtra("imagelist");
mPosition = getIntent().getIntExtra("position", 0);
if (mImageList == null || mImageList.isEmpty()) {
finish();
return;
}
initView();
initListener();
}
/**
* 5.0以上切换NavigationBar颜色
* !!!注意!!!需要在Activity的onCreate()之前调用以下方法
*/
public static void changeNavigationBarColor(Activity activity, int color)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Window window = activity.getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(color);//切换导航栏颜色
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean setSystemBarColor(
Window window,
int color,
boolean expand) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
if (expand) {
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
window.setStatusBarColor(color);
window.setNavigationBarColor(color);
return true;
}
public static void setTranslucentStatusBar(Activity activity){
Window window = activity.getWindow();
View view = window.getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
view.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
/**
* Set the navigation bar color.
*/
public static void setNavigationBarColor(Activity activity, int navigationBarColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(navigationBarColor);
}
}
/**
* Reset the color to default (black)
* @param window Window
*/
public static void resetBarsColor(Window window) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(ContextCompat.getColor(window.getContext(), R.color.black));
window.setNavigationBarColor(ContextCompat.getColor(window.getContext(), R.color.black));
}
}
public void setFullScreen() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
@SuppressWarnings("RestrictTo")
public void applyEdgeToEdgePreference(Window window) {
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
return;
}
boolean edgeToEdgeEnabled = isEdgeToEdgeEnabled();
int statusBarColor = getStatusBarColor(isEdgeToEdgeEnabled());
int navbarColor = getNavBarColor(isEdgeToEdgeEnabled());
boolean lightBackground = isColorLight(
MaterialColors.getColor(context, android.R.attr.colorBackground, Color.BLACK));
boolean lightNavbar = isColorLight(navbarColor);
boolean showDarkNavbarIcons = lightNavbar || (navbarColor == TRANSPARENT && lightBackground);
View decorView = window.getDecorView();
int currentStatusBar = VERSION.SDK_INT >= VERSION_CODES.M
? decorView.getSystemUiVisibility() & SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
: 0;
int currentNavBar = showDarkNavbarIcons && VERSION.SDK_INT >= VERSION_CODES.O
? SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
: 0;
window.setNavigationBarColor(navbarColor);
window.setStatusBarColor(statusBarColor);
int systemUiVisibility = (edgeToEdgeEnabled ? EDGE_TO_EDGE_FLAGS : SYSTEM_UI_FLAG_VISIBLE)
| currentStatusBar
| currentNavBar;
decorView.setSystemUiVisibility(systemUiVisibility);
}
/**
* 设置状态栏透明
*/
private void setStatusBar(){
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
private void setStatusBar(){
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
private void setStatusBar(){
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void applyStatusNavigationTranslucency() {
if (C.API_LOLLIPOP) {
Window window = getWindow();
int color = ACTION_BAR_COLOR;
window.setStatusBarColor(color);
window.setNavigationBarColor(color);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
@TargetApi(21)
public static void setNavBarColor(Window window, Resources.Theme theme, Integer color) {
if (Build.VERSION.SDK_INT >= 21) {
if (color == null) {
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(android.R.attr.navigationBarColor, typedValue, true);
color = typedValue.data;
}
window.setNavigationBarColor(color);
}
}
private void setStatusBar(){
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
private void clearNavigationBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && mSettings.getCurrent().mUseMatchingNavbarColor) {
final Window window = getWindow().getWindow();
if (window == null) {
return;
}
window.setNavigationBarColor(mOriginalNavBarColor);
final View view = window.getDecorView();
view.setSystemUiVisibility(mOriginalNavBarFlags);
}
}
@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());
}
/**
* Set the navigation bar's color.
*
* @param window The window.
* @param color The navigation bar's color.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public static void setNavBarColor(@NonNull final Window window, @ColorInt final int color) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(color);
}