話不多說一看就懂,網上的大部分實現都沒效果,用這個就對了
public class FullScreenDialog extends Dialog {
private View view;
public FullScreenDialog(@NonNull Context context, View view) {
super(context);
this.view = view;
}
public FullScreenDialog(@NonNull Context context, int themeResId, View view) {
super(context, themeResId);
this.view = view;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(view);
//按空白處不能取消
setCanceledOnTouchOutside(false);
Window window = getWindow();
View decorView = window.getDecorView();
//設置window背景,默認的背景會有Padding值,不能全屏。當然不一定要是透明,你可以設置其他背景,替換默認的背景即可。
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//兩個 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);
//內容擴展到導航欄
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
}
這麼用->
View view = LayoutInflater.from(context).inflate(R.layout.dialog_face_check, null);
Dialog dialog = new FullScreenDialog(activity,
android.R.style.Theme_DeviceDefault_Light_NoActionBar, view);
dialog.setCancelable(true);
dialog.setOnCancelListener(dialog1 -> {
});
dialog.show();