目录

花式实现全屏Dialog

目录

  • 自定义Dialog有多重方式,如集成Dialog,还有官方推荐的DialogFragment;
  • 下面将贴出多种方式实现全屏Dialog的代码

  • 1、继承Dialog

    public class FullScrreenDialog extends Dialog {
        public FullScrreenDialog(Context context) {
            super(context);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //取消title的显示
            getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            View view = LayoutInflater.from(getContext()).inflate(R.layout.you_layout, null);
            setContentView(view);
            //设置背景透明
            getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
            //设置layout参数撑满父布局
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }
    }
    
  • 2、DialogFragment实现方式

    public class FullScreenDialogFragment extends DialogFragment {
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_full_screen, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
          //取消title的显示
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            super.onActivityCreated(savedInstanceState);
           //设置背景透明
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
             //设置layout参数撑满父布局
            getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }
    
    }