SatusBarUtil工具类:
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.example.qixinandroid_by_yh.R;
//透明色完全沉浸
public class SatusBarUtil {
public static void setImmersion(Window window){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.flags |= flagTranslucentNavigation;
window.setAttributes(attributes);
window.setStatusBarColor(Color.TRANSPARENT);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
// Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
window.setAttributes(attributes);
}
}
}
/**
* 设置状态栏字体颜色
*
* @param isDark
*/
@SuppressLint("ResourceAsColor")
public static void setStatusTextColor(boolean isDark, Window window, @ColorInt int color) {
if (isDark) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//黑色字体
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
设置颜色
window.setStatusBarColor(color);
}
} else {
//白色字体
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
使用:

完全沉浸式

设置状态栏白色

本文详细介绍了StatusBarUtil工具类的使用方法,包括如何实现状态栏的完全沉浸式效果及设置状态栏字体颜色。适用于Android应用开发者,特别是关注用户体验与界面美观的开发人员。

被折叠的 条评论
为什么被折叠?



