| 
  
 Android新手开发之旅目录
 
 
  
 
 
 1、工具类
 
 [Java] 纯文本查看 复制代码 
/**
 * Created by Administrator on 2019/1/17.
 */
public class MobileStatusBarColorUtils {
    public static void setColor(Activity activity, int color) {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = new View(window.getContext());
        int statusBarHeight = getStatusBarHeight(activity);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
        params.gravity = Gravity.TOP;
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(color);
        decorViewGroup.addView(statusBarView);
    }
    private static int getStatusBarHeight(Activity activity) {
        int statusBarHeight = 0;
        Resources res = activity.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
}
 2、在需要修改状态栏的界面中使用
 
 [C#] 纯文本查看 复制代码  //传入想设置的颜色即可
        MobileStatusBarColorUtils.setColor(this, getResources().getColor(R.color.colorAccent));
 3、布局跟标签添加
 
 [XML] 纯文本查看 复制代码 android:fitsSystemWindows="true"
 效果如下图:
 
 
   
 
 |