zoukankan      html  css  js  c++  java
  • 一行代码使Android状态栏变沉浸式透明化

    public static void setStatusBarColor(Activity activity, int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    ViewGroup decorViewGroup = (ViewGroup) activity.getWindow().getDecorView();
    //获取自己布局的根视图
    View rootView = ((ViewGroup) (decorViewGroup.findViewById(android.R.id.content))).getChildAt(0); //预留状态栏位置
    rootView.setFitsSystemWindows(true); //添加状态栏高度的视图布局,并填充颜色
    View statusBarTintView = new View(activity);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
    PhoneInfo.getInternalDimensionSize(activity.getResources(), "status_bar_height"));
    params.gravity = Gravity.TOP;
    statusBarTintView.setLayoutParams(params);
    statusBarTintView.setBackgroundColor(color);
    decorViewGroup.addView(statusBarTintView);
    }
    } public static int getInternalDimensionSize(Resources res, String key) {
    int result = 0; int resourceId = res.getIdentifier(key, "dimen", "android");
    if (resourceId > 0) {
    result = res.getDimensionPixelSize(resourceId);
    }
    return result;
    }
    }

    以后就只需要在Activity中添加这一行代码,不用修改其他地方,第一个参数为Activity,第二个为颜色Id:

    PhoneInfo.setStatusBarColor(this,getResources().getColor(android.R.color.holo_blue_light));

    注意一定要设置在setContentView()方法之后,如:

    setContentView(R.layout.layout);  
    PhoneInfo.setStatusBarColor(this, getResources().getColor(R.color.blue));
  • 相关阅读:
    关于最大最小的k个数的类型题总结
    最小的K个数(剑指offer)
    215. Kth Largest Element in an Array(返回数组中第几大元素)(leetcode)
    数组中出现次数超过一半的数字(剑指offer)
    二叉搜索树与双向链表(剑指offer)
    第四届蓝桥杯c/c++B组6
    第四届蓝桥杯c/c++B组7
    第四届蓝桥杯c/c++B组8
    第四届蓝桥杯c/c++B组9
    第五届蓝桥杯 c/c++ B组8
  • 原文地址:https://www.cnblogs.com/cold-ice/p/7825929.html
Copyright © 2011-2022 走看看