網(wǎng)上有很這方面的解決方案蛀醉,我要分享的是相對簡單實(shí)用的一種方案著摔,適合救急可直接放入項(xiàng)目中使用揖赴,希望能幫助到您馆匿,只需要幾步就能解決(親測絕對靠譜可放心在項(xiàng)目中使用兼容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.狀態(tài)欄兼容類
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);
//改變顏色時避免重復(fù)添加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.創(chuàng)建Activity燥滑、FragmentActivity基類方便其它頁面繼承
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusBar(this);
}
/**
* 設(shè)置狀態(tài)顏色
* @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);
}
/**
* 設(shè)置狀態(tài)顏色
* @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"屬性