Android状态栏和导航栏

1.隐藏状态栏或导航栏

        View decordView = getWindow().getDecorView();
        /*SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN 分别代表隐藏导航栏和状态栏
        * SYSTEM_UI_FLAG_IMMERSIVE_STICKY 沉浸式效果*/
        decordView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        getSupportActionBar().hide();
    }

2. 使状态栏透明

1.通过代码设置
      View decordView = getWindow().getDecorView();
        decordView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            );
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(Color.GREEN);
            getWindow().setNavigationBarColor(Color.GREEN);
 
        } 

 ps:    getSupportActionBar().hide();不要调用hide而是 在xml中的style中直接使用Theme.AppCompat.Light.NoActionBar

并且在layout中设置android:fitsSystemWindows="true" 可以让布局布局在状态栏下面而不是覆盖状态栏

2.通过主题设置

api>=21

    
<style name="TranslucentStatus" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:windowTranslucentStatus">false</item><!--如果为true导航栏为半透明--> <item name="android:windowTranslucentNavigation">true</item> </style>

api 19

 <style name="TranslucentStatus" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item
</style>

猜你喜欢

转载自www.cnblogs.com/cnman/p/9897866.html
今日推荐