Created
September 18, 2017 02:54
-
-
Save 410063005/2c31ae6c2bc53e7e568375e184e922a6 to your computer and use it in GitHub Desktop.
修改状态栏颜色
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by zhy on 15/9/21. | |
* http://blog.csdn.net/lmj623565791/article/details/48649563 | |
*/ | |
public class StatusBarCompat | |
{ | |
private static final int INVALID_VAL = -1; | |
private static final int COLOR_DEFAULT = Color.parseColor("#20000000"); | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public static void compat(Activity activity, int statusColor) | |
{ | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) | |
{ | |
if (statusColor != INVALID_VAL) | |
{ | |
activity.getWindow().setStatusBarColor(statusColor); | |
} | |
return; | |
} | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) | |
{ | |
int color = COLOR_DEFAULT; | |
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); | |
if (statusColor != INVALID_VAL) | |
{ | |
color = statusColor; | |
} | |
View statusBarView = new View(activity); | |
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, | |
getStatusBarHeight(activity)); | |
statusBarView.setBackgroundColor(color); | |
contentView.addView(statusBarView, lp); | |
} | |
} | |
public static void compat(Activity activity) | |
{ | |
compat(activity, INVALID_VAL); | |
} | |
public static int getStatusBarHeight(Context context) | |
{ | |
int result = 0; | |
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); | |
if (resourceId > 0) | |
{ | |
result = context.getResources().getDimensionPixelSize(resourceId); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment