下面列出了android.view.Window#setStatusBarColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 设置状态栏颜色,并改变状态栏字体颜色
* @param activity 当前所在的activity
* @param color 想要设置的颜色
*/
public static void setStatusBar(Activity activity, int color){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){//小于5.0不做处理
return;
}
Window window = activity.getWindow();
window.setStatusBarColor(color);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ // 小于6.0字体不做处理
return;
}
if (isLightColor(color)){
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}else{
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
public static void setupWindowFlagsForStatusbarOverlay(Window window, boolean setBackgroundResource) {
if (MizLib.isKitKat()) {
// If we're running on KitKat, we want to enable
// the translucent status bar
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
if (MizLib.hasKitKat()) {
// If we're running on KitKat or above, we want to show
// the background image beneath the status bar as well.
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
// Make the status bar color transparent to begin with
if (MizLib.hasLollipop())
window.setStatusBarColor(Color.TRANSPARENT);
// If requested, set a background resource on the Window object
if (setBackgroundResource)
window.setBackgroundDrawableResource(R.drawable.bg);
}
public static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
window.clearFlags(NTLMConstants.FLAG_UNIDENTIFIED_9);
window.addFlags(Integer.MIN_VALUE);
window.setStatusBarColor(statusColor);
window.getDecorView().setSystemUiVisibility(0);
View mChildView = ((ViewGroup) window.findViewById(16908290)).getChildAt(0);
if (mChildView != null) {
ViewCompat.setOnApplyWindowInsetsListener(mChildView, new OnApplyWindowInsetsListener() {
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
return insets;
}
});
ViewCompat.setFitsSystemWindows(mChildView, true);
ViewCompat.requestApplyInsets(mChildView);
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
overridePendingTransition(R.anim.left_in, 0);
super.onCreate(savedInstanceState);
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_STABLE
);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
mSwipeBackLayout = new SwipeBackLayout(this);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, new SettingsFragment());
ft.commit();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
/**
* Flag只有在使用了FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
* 并且没有使用 FLAG_TRANSLUCENT_STATUS 的时候才有效,也就是只有在状态栏全透明的时候才有效。
*
* @param window
* @param bDark bDark为true时是黑色的字,为false时是白色的字
*/
private static void setStatusBarModeDefault(Window window, boolean bDark) {
//6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (window == null) {
return;
}
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ColorUtils.blendARGB(Color.TRANSPARENT, Color.BLACK, 0.0f));
View decorView = window.getDecorView();
if (decorView != null) {
int vis = decorView.getSystemUiVisibility();
vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
}
private void setInit() {
//沉浸状态栏
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//Android 系统5.0一下
} else {
//Android 系统5.0一上
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(0xff000000);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void systemBarLolipop(Activity act) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Window window = act.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(act.getResources().getColor(R.color.colorPrimaryDark));
}
}
/**
* Set the status bar to a particular color
*
* @param color color resource id
*/
@Override
public void setStatusBarColor(int color) {
// we can only set it 5.x and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, color));
}
}
/**
* <全透状态栏>
* 全透明状态栏,fitsSystemWindows=false
* setStatusBarFullTransparent();
* setFitSystemWindow(false);
* 全透明状态栏,fitsSystemWindows=true
* setStatusBarFullTransparent();
* setFitSystemWindow(true);
* </全透状态栏>
*/
protected void setStatusBarFullTransparent() {
if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//虚拟键盘也透明
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 修改状态栏颜色,支持4.4以上版本
* @param activity
* @param colorId
*/
public static void setStatusBarColor(Activity activity,int colorId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(colorId));
}
}
/**
* 设置系统状态栏颜色
*/
public static void setStatusColor(Window window, int color, @FloatRange(from = 0.0, to = 1.0) float alpha) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(mixtureColor(color, alpha));
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
@Override
public void onDestroyView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getActivity().getWindow();
window.setStatusBarColor(statusBarColor);
}
super.onDestroyView();
}
@Override
public void onCreate(Bundle savedInstanceState) {
SharedPreferences Sp = PreferenceManager.getDefaultSharedPreferences(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.prefsfrag);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (SDK_INT >= 21) {
ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription("Amaze",
((BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher)).getBitmap(),
getColorPreference().getColor(ColorUsage.PRIMARY));
setTaskDescription(taskDescription);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_HOME_AS_UP | android.support.v7.app.ActionBar.DISPLAY_SHOW_TITLE);
getSupportActionBar().setBackgroundDrawable(getColorPreference().getDrawable(ColorUsage.PRIMARY));
if (SDK_INT == 20 || SDK_INT == 19) {
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintColor(getColorPreference().getColor(ColorUsage.PRIMARY));
FrameLayout.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) findViewById(R.id.preferences).getLayoutParams();
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
p.setMargins(0, config.getStatusBarHeight(), 0, 0);
} else if (SDK_INT >= 21) {
boolean colourednavigation = Sp.getBoolean("colorednavigation", true);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(PreferenceUtils.getStatusColor(getColorPreference().getColorAsString(ColorUsage.PRIMARY)));
if (colourednavigation)
window.setNavigationBarColor(PreferenceUtils.getStatusColor(getColorPreference().getColorAsString(ColorUsage.PRIMARY)));
}
if (savedInstanceState != null){
selectedItem = savedInstanceState.getInt(KEY_CURRENT_FRAG_OPEN, 0);
}
selectItem(selectedItem);
}
public static void setStatusBarColor(Window window, @ColorInt int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
}
}
private void changeStatusBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}
public void setWindowStatusBarColor(Activity activity, int colorResId) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(colorResId));
//window.setNavigationBarColor(activity.getResources().getColor(colorResId));
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* change to full screen mode
*
* @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
//set child View not fill the system window
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight(activity);
//First translucent status bar.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//After LOLLIPOP just set LayoutParams.
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(COLOR_TRANSLUCENT);
} else {
window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
}
//must call requestApplyInsets, otherwise it will have space in screen bottom
if (mChildView != null) {
ViewCompat.requestApplyInsets(mChildView);
}
} else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean &&
(Boolean) mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
//remove fake status bar view.
mContentView.removeView(mChildView);
mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
//cancel the margin top
if (lp != null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
mChildView.setLayoutParams(lp);
}
}
mDecorView.setTag(false);
}
}
}
}
private void applyTheme() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.setStatusBarColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_note);
overridePendingTransition(R.anim.slide_in_from_right,R.anim.slide_in_from_right);
Window window = InternalNoteActivity.this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(InternalNoteActivity.this,R.color.faveo));
toolbar= (Toolbar) findViewById(R.id.toolbarForInternalNote);
imageView= (ImageView) toolbar.findViewById(R.id.imageViewBackTicketInternalNote);
editTextInternalNote = (EditText) findViewById(R.id.editText_internal_note);
buttonCreate = (Button) findViewById(R.id.button_create);
ticketID= Prefs.getString("TICKETid",null);
progressDialog=new ProgressDialog(this);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
buttonCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String note = editTextInternalNote.getText().toString();
if (note.trim().length() == 0) {
Toasty.warning(InternalNoteActivity.this, getString(R.string.msg_must_not_be_empty), Toast.LENGTH_LONG).show();
return;
}
String userID = Prefs.getString("ID", null);
if (userID != null && userID.length() != 0) {
try {
note = URLEncoder.encode(note, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
new CreateInternalNoteForTicket(Integer.parseInt(ticketID), Integer.parseInt(userID), note).execute();
progressDialog.setMessage(getString(R.string.creating_note));
progressDialog.show();
} else
Toasty.warning(InternalNoteActivity.this, getString(R.string.wrong_user_id), Toast.LENGTH_LONG).show();
}
});
}
private Target<Bitmap> getLightThemeTarget() {
return new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
streamerImage.setImageBitmap(bitmap);
Palette palette = Palette.from(bitmap).generate();
int defaultColor = Service.getColorAttribute(R.attr.colorPrimary, R.color.primary, getBaseContext());
int defaultDarkColor = Service.getColorAttribute(R.attr.colorPrimaryDark, R.color.primaryDark, getBaseContext());
int vibrant = palette.getVibrantColor(defaultColor);
int vibrantDark = palette.getDarkVibrantColor(defaultColor);
int vibrantLight = palette.getLightVibrantColor(defaultColor);
int muted = palette.getMutedColor(defaultColor);
int mutedDark = palette.getDarkMutedColor(defaultColor);
Palette.Swatch swatch;
if (vibrant != defaultColor) {
swatch = palette.getVibrantSwatch();
} else if (vibrantDark != defaultColor) {
swatch = palette.getDarkVibrantSwatch();
} else if (vibrantLight != defaultColor) {
swatch = palette.getLightVibrantSwatch();
} else if (muted != defaultColor) {
swatch = palette.getMutedSwatch();
} else if (mutedDark != defaultColor) {
swatch = palette.getDarkMutedSwatch();
} else {
swatch = palette.getLightMutedSwatch();
}
if (swatch != null) {
float[] swatchValues = swatch.getHsl();
float[] newSwatch = {swatchValues[0], (float) 0.85, (float) 0.85};
float[] newSwatchComposite = {(swatchValues[0] + 180) % 360, newSwatch[1], newSwatch[2]};
float[] newSwatchDark = {newSwatch[0], newSwatch[1], (float) 0.6};
int newColorDark = Color.HSVToColor(newSwatchDark);
int newColor = Color.HSVToColor(newSwatch);
int compositeNewColor = Color.HSVToColor(newSwatchComposite);
int primaryColor = Service.getBackgroundColorFromView(toolbar, defaultColor);
int primaryColorDark = Service.getBackgroundColorFromView(mTabs, defaultDarkColor);
Service.animateBackgroundColorChange(toolbar, newColor, primaryColor, COLOR_FADE_DURATION);
Service.animateBackgroundColorChange(additionalToolbar, newColor, primaryColor, COLOR_FADE_DURATION);
Service.animateBackgroundColorChange(mTabs, newColorDark, primaryColorDark, COLOR_FADE_DURATION);
mFab.setBackgroundTintList(ColorStateList.valueOf(compositeNewColor));
mTabs.setSelectedTabIndicatorColor(compositeNewColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(newColorDark);
}
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
};
}