Android两行代码修改给系统状态栏设置透明效果

在实际开发中为了提高整体的视觉,有时候会把系统栏设置为自定义的颜色。或者跟随系统。
效果图

1、Android中给我们提供的有方法

比如:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

在设置完View布局之后调用该方法,就可以实现状态栏的改变,但是运行会发现,这样把整体的布局都上移了。相当于inVisible和Gone的区别。
但是要实现只隐藏的效果还需要在xml根布局的父控件中添加一行

android:fitsSystemWindows="true"

这样虽然可以实现同样的效果,但是每建一个Activity都需要拷贝大量的代码。不利于查看。

2、也可以使用一行代码搞定

这个是我们当前项目中使用的方法。

X_SystemBarUI.initSystemBar(this,R.color.colorPrimary);

调用该方法需要定义两个类

SystemBarTintManager.java
X_SystemBarUI.java

部分代码

/** 
     * 改变系统标题栏颜色 
     * @param activity 
     * @param color   color xml文件下的颜色 
     */  
    public static void initSystemBar(Activity activity, int color) {  
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
            setTranslucentStatus(activity, true);  
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(activity);  
        tintManager.setStatusBarTintEnabled(true);  
        // 使用颜色资源  
        tintManager.setStatusBarTintResource(color);  

    }  
    /** 
     * 设置系统标题栏的透明度 
     * @param activity 
     * @param on 
     */  
    @TargetApi(19)  
    private static void setTranslucentStatus(Activity activity, boolean on) {  
        Window win = activity.getWindow();  
        WindowManager.LayoutParams winParams = win.getAttributes();  
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;  
        if (on) {  
            winParams.flags |= bits;  
        } else {  
            winParams.flags &= ~bits;  
        }  
        win.setAttributes(winParams);  
    }  
3、这个是网上找的一个方法
new SystemStatusManager(this).setTranslucentStatus(R.color.colorPrimary);

只需要定义一个类:

SystemStatusManager.java

部分代码:

 public void setTranslucentStatus(int res) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            this.mContext.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            this.mContext.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemStatusManager tintManager = new SystemStatusManager(this.mContext);
            tintManager.setStatusBarTintEnabled(true);
            // 设置状态栏的颜色
            tintManager.setStatusBarTintResource(res);
            this.mContext.getWindow().getDecorView().setFitsSystemWindows(true);
        }
    }

    static {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
                sNavBarOverride = null;
            }
        }
    }

这个方法中还有一个去除标题栏和状态栏之间黑线的功能
具体使用:在appTheme主题中加入:

<item name="android:windowContentOverlay">@null</item>

这三个类都被我放在了github上,直接传送过去复制就行

源码地址

最后别忘了在父控件中添加一行

android:fitsSystemWindows="true"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值