情況一:保留狀態欄 只是將我們的布局嵌入到狀態欄中
方法一:通過設置theme主題
因為 API21 之后(也就是 android 5.0 之后)的狀態欄,會默認覆蓋一層半透明遮罩。且為了保持4.4以前系統正常使用,故需要三份 style 文件,即默認的values(不設置狀態欄透明)、values-v19、values-v21(解決半透明遮罩問題)。
//valuse <style name="TranslucentTheme" parent="AppTheme"> </style> // values-v19。v19 開始有 android:windowTranslucentStatus 這個屬性 <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item> </style> // values-v21。5.0 以上提供了 setStatusBarColor() 方法設置狀態欄顏色。 <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">true</item> <!--Android 5.x開始需要把顏色設置透明,否則導航欄會呈現系統默認的淺灰色--> <item name="android:statusBarColor">@android:color/transparent</item> </style>
然后在清單文件中將theme設置給對應的Activity即可。
方法二:代碼設置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.flags |= flagTranslucentNavigation; window.setAttributes(attributes); getWindow().setStatusBarColor(Color.TRANSPARENT); } else { Window window = getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation; window.setAttributes(attributes); } }
注:但是從圖片中也看到了,該方案會導致一個問題就是導航欄顏色變灰。經測試,在5.x以下導航欄透明是可以生效的,但是5.x以上的導航欄會變灰色(正常情況下我們期望導航欄保持默認黑色不變),但是因為設置了FLAG_TRANSLUCENT_NAVIGATION,所以即時代碼中設置了getWindow().setNavigationBarColor(Color.BLACK);此時也是不起作用的。但是如果不設置該FLAG,狀態欄又無法設置為隱藏和設置透明。綜上還是推薦第一種。
情況二:去掉狀態欄
方法一:代碼設置隱藏狀態欄
/** * 通過設置全屏,設置狀態欄透明 * * @param activity */ private void fullScreen(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.x開始需要把顏色設置透明,否則導航欄會呈現系統默認的淺灰色 Window window = activity.getWindow(); View decorView = window.getDecorView(); //兩個 flag 要結合使用,表示讓應用的主體內容占用系統狀態欄的空間 int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); //導航欄顏色也可以正常設置 // window.setNavigationBarColor(Color.TRANSPARENT); } else { Window window = activity.getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; attributes.flags |= flagTranslucentStatus; // attributes.flags |= flagTranslucentNavigation; window.setAttributes(attributes); } } }
最后 附上大神的博客和github
http://www.jianshu.com/p/dc20e98b9a90
https://github.com/yazhi1992/Practice
