在setContentView以前加上以下這段代碼便可。android
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//設置狀態欄透明,沉浸方案 //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//透明導航欄 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ try { Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView"); Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor"); field.setAccessible(true); field.setInt(getWindow().getDecorView(), Color.TRANSPARENT); //改成透明 } catch (Exception e) {} } setContentView(R.layout.activity_main);
代碼參考:https://blog.csdn.net/hmmhhmmhmhhm/article/details/77840604ui
效果圖:spa