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));
  • 相关阅读:
    解决NLPIR汉语分词系统init failed问题
    牛客小白月赛3---G 旅游(树形dp)
    蓝桥杯 能量项链 (区间dp)
    OpenJ_Bailian
    LeetCode#169 Majority Element
    LeetCode#171 Excel Sheet Column Number
    LeetCode#172 Factorial Trailing Zeroes
    this指针
    auto、register、extern以及static
    const与static
  • 原文地址:https://www.cnblogs.com/cold-ice/p/7825929.html
Copyright © 2011-2022 走看看