Android沉浸式状态栏(透明状态栏)

来源:互联网 发布:功夫软件 编辑:程序博客网 时间:2024/05/15 06:28

这是个老话题了,不纠结叫法了。勿怒,一起来看看那些坑。


从android 4.4起,系统提供了这个功能。到5.0之后,系统有改进了,更好用了。

//LOLLIPOP   21  5.0        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            View decorView = activity.getWindow().getDecorView();            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                //状态栏浅色字体                option |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;            }            decorView.setSystemUiVisibility(option);            activity.getWindow().setStatusBarColor(statusColor);            return;        }

5.0以上系统,只需要这段代码即可,直接修改状态栏颜色。

但是对于4.4~5.0之间的系统,不好弄。网上各位大神提供了很好的建议,说创建一个与状态栏等高的view,色后缀颜色不就可以了。真的?来看效果图。


这是怎么了,我的二锅头。 有人说,来个marginTop或者在当前Activity的根布局加代码android:fitsSystemWindows="true"不就可以了吗! 真的?来看图:





虽然这颜色很刺眼,但是效果还是很明显的。不成。于是我这么干的:

//KITKAT   19  4.4        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            View decorView = activity.getWindow().getDecorView();            int option = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;//透明状态栏            option |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;//留出状态栏空间            decorView.setSystemUiVisibility(option);            decorView.setBackgroundColor(statusColor);        }
还可以,只是效果有一点差异,即状态栏的颜色与正宗的颜色有一点灰色透明度差异。略加黑了,也是公认的效果了。




下面贴一下工具类:

/** * @Author: duke * @DateTime: 2017-06-15 09:51 * @Description: 沉浸式状态栏工具类 <br/> * Your should decide if the root layout need to add property android:fitsSystemWindows="true" */public class ImmersiveStatusBarCompat {    public static void immersive(Activity activity) {        immersive(activity, Color.TRANSPARENT);    }    /**     * @param activity     * @param statusColor 状态栏颜色     */    public static void immersive(Activity activity, int statusColor) {        if (activity == null) {            throw new IllegalArgumentException("activity is null");        }        //LOLLIPOP   21  5.0        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            View decorView = activity.getWindow().getDecorView();            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                //状态栏浅色字体                option |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;            }            decorView.setSystemUiVisibility(option);            activity.getWindow().setStatusBarColor(statusColor);            return;        }        //KITKAT   19  4.4        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            View decorView = activity.getWindow().getDecorView();            int option = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;//透明状态栏            option |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;//留出状态栏空间            decorView.setSystemUiVisibility(option);            decorView.setBackgroundColor(statusColor);        }    }    public static void hideNavigation(Activity activity) {        if (activity == null) {            throw new IllegalArgumentException("activity is null");        }        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            View decorView = activity.getWindow().getDecorView();            int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;            decorView.setSystemUiVisibility(option);            activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);            return;        }    }    /**     * 获取状态栏高度     *     * @param context     * @return     */    public static int getStatusBarHeight(Context context) {        if (context == null) {            return 0;        }        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");        return context.getResources().getDimensionPixelSize(resourceId);    }}


用法呢,一样简单:

layout跟布局加属性:android:fitsSystemWindows="true"在activity中@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_immersive);    ImmersiveStatusBarCompat.immersive(this, Color.parseColor("#00ff00"));}










阅读全文
0 0