网上有很这方面的解决方案,我要分享的是相对简单实用的一种方案,适合救急可直接放入项目中使用,希望能帮助到您,只需要几步就能解决(亲测绝对靠谱可放心在项目中使用兼容19以下)。
话不多说干货来了
1.自定义主题在styles.xml中
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/title_bar</item>
<item name="colorPrimaryDark">@color/title_bar</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
2.状态栏兼容类
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 = contentView.getChildAt(0);
//改变颜色时避免重复添加statusBarView
if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) {
statusBarView.setBackgroundColor(color);
return;
}
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;
}
}
3.创建Activity、FragmentActivity基类方便其它页面继承
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusBar(this);
}
/**
* 设置状态颜色
* @param act
*/
public void setStatusBar(Activity act){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
StatusBarCompat.compat(act, Color.parseColor("#34A8E5"));
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
setStatusBar(this);
}
super.onWindowFocusChanged(hasFocus);
}
}
public class BaseFragmentActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusBar(this);
}
/**
* 设置状态颜色
* @param act
*/
public void setStatusBar(Activity act){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
StatusBarCompat.compat(act, Color.parseColor("#34A8E5"));
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
setStatusBar(this);
}
super.onWindowFocusChanged(hasFocus);
}
}
4.页面布局中需要加入 android:fitsSystemWindows="true"属性