Android 4.4新特性之透明状态栏和透明虚拟导航栏

参考资料:

http://www.cnblogs.com/zhengxt/p/3536905.html

http://www.xuebuyuan.com/2207437.html

http://www.jcodecraeer.com/a/opensource/2014/1222/2198.html

http://www.eoeandroid.com/thread-560459-1-1.html?_dsign=acfc8bcd

 

除了沉浸模式外,Android 4.4还有新的API,能使应用内的状态栏和虚拟按钮透明。如果要使应用内的状态栏和虚拟按钮变成透明有两种方法。

一种是代码方式:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //下面这句话要写在setContentView之前
        //getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //设定状态栏的颜色,当版本大于4.4时起作用
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
            tintManager.setTintColor(Color.BLUE);

            //SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
            //map.setPadding(0, config.getPixelInsetTop(), config.getPixelInsetRight(), config.getPixelInsetBottom());
        }

        if (getSupportActionBar() != null){
            if (getSupportActionBar().isShowing()){
                getSupportActionBar().hide();
            }
            else {
                getSupportActionBar().show();
            }
        }
    }


通过

compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

可以在android4.4版本改变状态栏和虚拟导航栏的颜色。这是个4.4的替代方案,在5.0以后有新的API,就不需要再使用这个库,所以做好版本判断。

注意在布局文件头加入如下句:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.badu.immersivemode.MainActivity">

 

在样式文件里添加这俩句也可以达到透明的效果。

        <!-- <item name="android:windowTranslucentNavigation" >true</item>
        <item name="android:windowTranslucentStatus">true</item> -->

 

 

另外一种是使用两个新的主题风格:

Theme.Holo.NoActionBar.TranslucentDecorTheme.Holo.Light.NoActionBar.TranslucentDecor

但是这种方式只支持Android4.4以上系统,所以为了保持兼容性,我们还是采用代码方式比较好。只需要先判断,如果是4.4以上系统才启用代码。

 

posted @ 2016-04-12 11:50  因为全栈所以架构  阅读(573)  评论(0编辑  收藏  举报