Android DialogFragmentの概要

5823 ワード

title:「Android DialogFragment」subtitle:「"Android DialogFragment簡単に使える""date:2015-08-21 12:00:00 tags:-Android学習ノート-DialogFragment
微博:陳喬陳先森
転載明記出典:陳喬陳先森:Android TabLayout
DialogFragment簡単に使用
Googleでは、DialogFragmentを使用してダイアログボックスを管理することを推奨しています.画面を回転したり、戻るキーを押したりすると、ライフサイクル管理dialogをよりよく利用することができます.これはFragmentとほぼ一致する宣言サイクルを持っています.DialogFragmentは、少なくともonCreateViewまたはonCreateDIalogの方法のうちの1つを実装する必要がある.定義されたxmlレイアウトファイルを使用して、onCreateViewメソッドでDialogを表示します.onCreateDialog AlertDialogまたはDialogを使用してDialogを作成
  • onCreateViewメソッド、すなわち定義xmlレイアウトファイルを用いてDialog
  • を示す.
        public class MyDialogFragment extends DialogFragment  {  
            @Override  
            public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                    Bundle savedInstanceState){  
                //       
                View view = inflater.inflate(R.layout.fragment_dialog, container);  
                return view;  
            }  
        } 
    

    ポップアップダイアログ:
        MyDialogFragment mDialogFragment= new MyDialogFragment ();  
        mDialogFragment.show(getFragmentManager(), "MyDialogment");
    
  • onCreateDialog AlertDialogまたはDialogBuilderを使用してDialog
  • を作成
    public class MyDialogFragment extends DialogFragment  
    {  
      
        @Override  
        public Dialog onCreateDialog(Bundle savedInstanceState)  
        {  
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
            // Get the layout inflater  
            LayoutInflater inflater = getActivity().getLayoutInflater();  
            View view = inflater.inflate(R.layout.fragment_dialog, null);  
            // Inflate and set the layout for the dialog  
            // Pass null as the parent view because its going in the dialog layout  
            builder.setView(view)  
                    // Add action buttons  
                    .setPositiveButton("Sign in",  
                            new DialogInterface.OnClickListener()  
                            {  
                                @Override  
                                public void onClick(DialogInterface dialog, int id)  
                                {  
                                }  
                            }).setNegativeButton("Cancel", null);  
            return builder.create();  
        }  
    }  
    
    

    その呼び出し方法は同じです.
    fragmentとactivity間の通信についてはインタフェースコールバック方式を用いることができる
    開発中に発生した問題
    DialogFragmentデフォルトスタイルを削除し、ウィンドウの透明性を実現する
    方法1
  • onCreateDialogメソッドを書き換えるときにdialogスタイルを設定します.
  •     public class MyDialogFragment extends DialogFragment {
    
             @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
    
              Dialog m_dialog = new Dialog(getActivity(), R.style.Dialog_No_Border);
              LayoutInflater m_inflater = LayoutInflater.from(CustomDialogActivity.this);
              View v = LayoutInflater.from(mContext).inflate(R.layout.xxxx, null, false);
                //LayoutInflater inflater = getActivity().getLayoutInflater();
                // Get the layout inflater
                // view = inflater.inflate(R.layout.fragment_time_chooser, null);
                // Inflate and set the layout for the dialog
              m_dialog.setTitle(null);
              m_dialog.setContentView(v);
              m_dialog.show();
    
              return m_dialog;
             }
        }
    

    Dialog_の追加No_Borderスタイルはあなたのres/value/styleです.xmlファイルにあります.
        
    

    方法2:
  • onCreateviewメソッドを書き換える場合
  •     @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            final Window window = getDialog().getWindow();
            //        title
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            //  dialog    
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            view = inflater.inflate(R.layout.fragment_dialog, null);
            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            return view;
        }
    

    一般的な方法:
    Dialogは、getWindow()で取得します.DialogFragmentはgetDialog()である.getWindow()取得;
    //   
    mWindow.requestFeature(Window.FEATURE_NO_TITLE);// setContextView    
    
    rootView = (ViewGroup) inflater.inflate(R.layout.rsen_base_dialog_fragment_layout,
    (ViewGroup) mWindow.findViewById(android.R.id.content));
    //     
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    
    //  ,    
    mWindow.setWindowAnimations(getAnimStyles());
    
    //       
    mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    
    //  window         
    getDialog().setCanceledOnTouchOutside(canCanceledOnOutside());
    
    //      ,         
    setCancelable(canCancelable());
    
    //window     ,         
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    
    //      ,         ,      MATCH_PARENT
    mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    WindowManager.LayoutParams mWindowAttributes = mWindow.getAttributes();
    mWindowAttributes.width = getWindowWidth();//              ,      MATCH_PARENT
    mWindowAttributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
    
    //gravity
    mWindowAttributes.gravity = getGravity();
    mWindow.setAttributes(mWindowAttributes);
    

    またdialogfragmentレイアウトのサイズを制御するにはxmlで定義するのは役に立たず,様々な方法を試みたが,onResume()メソッドでしか実現できないことが分かった.
    public void onResume() {
          super.onResume();
          getDialog().getWindow().setLayout(750, 600);
      }