記一次dialog覆蓋在狀態(tài)欄和導(dǎo)航欄上方的解決方案:
private void setStatusBar(){
//按空白處不能取消
setCanceledOnTouchOutside(false);
Window window = getWindow();
window.setGravity(Gravity.CENTER);
//設(shè)置window背景,默認的背景會有Padding值,不能全屏。當(dāng)然不一定要是透明,你可以設(shè)置其他背景,替換默認的背景即可。
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
View decorView = window.getDecorView();
//兩個 flag 要結(jié)合使用,表示讓應(yīng)用的主體內(nèi)容占用系統(tǒng)狀態(tài)欄的空間
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
//設(shè)置導(dǎo)航欄顏
window.setNavigationBarColor(Color.TRANSPARENT);
//內(nèi)容擴展到導(dǎo)航欄
window.setType(2026);
}
@Override
public void show() {
super.show();
Window win = getWindow();
// 設(shè)置全屏,要設(shè)置在show的后面
WindowManager.LayoutParams layoutParams = win.getAttributes();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
win.setAttributes(layoutParams);
}
style.xml中(代碼中和xml中有的重復(fù)設(shè)置了,目前先這樣寫,后續(xù)調(diào)整):
<style name="FullscreenDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:fitsSystemWindows">true</item>
</style>