Dialog默認(rèn)無法全屏的原因

參考鏈接:https://blog.csdn.net/sydMobile/article/details/83588708?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control

記一次dialog覆蓋在狀態(tài)欄和導(dǎo)航欄上方的解決方案:

private void setStatusBar(){
    //按空白處不能取消
    setCanceledOnTouchOutside(false);
    Window window = getWindow();
    window.setGravity(Gravity.CENTER);
    //設(shè)置window背景,默認(rèn)的背景會(huì)有Padding值,不能全屏。當(dāng)然不一定要是透明,你可以設(shè)置其他背景,替換默認(rèn)的背景即可。
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    View decorView = window.getDecorView();
    //兩個(gè) 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)容擴(kuò)展到導(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>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容