1.设置沉浸式状态栏
public static void setStatusBarColor(Activity activity,int color){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ // 5.0 以上
activity.getWindow().setStatusBarColor(color); // 直接调用系统提供的方法 setStatusBarColor
}
// 4.4 - 5.0 之间 采用一个技巧,首先把他弄成全屏,在状态栏的部分加一个布局
else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
// 首先把他弄成全屏(),在状态栏的部分加一个布局
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 在状态栏的部分加一个布局 setContentView 源码分析,自己加一个布局 (高度是状态栏的高度)
View view = new View(activity);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity));
view.setLayoutParams(params);
view.setBackgroundColor(color);
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
decorView.addView(view);
// 获取activity中setContentView布局的根布局
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
contentView.setPadding(0,getStatusBarHeight(activity),0,0);
// View activityView = contentView.getChildAt(0);
// activityView.setPadding(0,getStatusBarHeight(activity),0,0);
// activityView.setFitsSystemWindows(true);
}
}
获取状态栏高度
// 获取状态栏的高度
private static int getStatusBarHeight(Activity activity) {
Resources resources = activity.getResources();
int statusBarHeightId = resources.getIdentifier("status_bar_height","dimen","android");
return resources.getDimensionPixelOffset(statusBarHeightId);
}
2.设置Activity全屏
public static void setActivityTranslucent(Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
3.修改状态栏字体颜色(白底黑字)
3.1 Android6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.WHITE);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
3.2Android4.4-6.0之间
3.2.1适配MIUI(小米)
public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
Class<? extends Window> clazz = activity.getWindow().getClass();
try {
int darkModeFlag = 0;
Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
3.2.2适配Flyme(魅族)
public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
boolean result = false;
if (activity != null) {
try {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
Field darkFlag = WindowManager.LayoutParams.class
.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
Field meizuFlags = WindowManager.LayoutParams.class
.getDeclaredField("meizuFlags");
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(lp);
if (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
activity.getWindow().setAttributes(lp);
result = true;
} catch (Exception e) {
}
}
return result;
}
3.2.3其他
// 我们将状态栏设置为一个淡灰色,例:支付宝,今日头条等
setStatusBarColor(activity,Color.parseColor("#33000000"))
4. 状态栏颜色渐变过渡算法
/**
* 合成新的颜色值
* @param fraction 颜色取值的级别 (0.0f ~ 1.0f)
* @param startValue 开始显示的颜色
* @param endValue 结束显示的颜色
* @return 返回生成新的颜色值
*/
public static int evaluate(float fraction, int startValue, int endValue) {
int startA = (startValue >> 24) & 0xff;
int startR = (startValue >> 16) & 0xff;
int startG = (startValue >> 8) & 0xff;
int startB = startValue & 0xff;
int endA = (endValue >> 24) & 0xff;
int endR = (endValue >> 16) & 0xff;
int endG = (endValue >> 8) & 0xff;
int endB = endValue & 0xff;
return ((startA + (int) (fraction * (endA - startA))) << 24) |
((startR + (int) (fraction * (endR - startR))) << 16) |
((startG + (int) (fraction * (endG - startG))) << 8) |
((startB + (int) (fraction * (endB - startB))));
}
网友评论