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>