Setting the size of a DialogFragment

前端 未结 4 1629
执笔经年
执笔经年 2020-12-07 19:13

I have been trying many commands to setup the size of my DialogFragment. It only contains a color-picker, so I have removed the background and title of the dial

4条回答
  •  伪装坚强ぢ
    2020-12-07 19:38

    After some trial and error, I have found the solution.

    here is the implementation of my DialogFragment class :

    public class ColorDialogFragment extends SherlockDialogFragment {
    
        public ColorDialogFragment() {
        //You need to provide a default constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_color_picker, container);
        // R.layout.dialog_color_picker is the custom layout of my dialog
        WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
        wmlp.gravity = Gravity.LEFT;
        return view;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.colorPickerStyle);
        // this setStyle is VERY important.
        // STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog
        // so for example the size of the default dialog will not get in my way
        // the style extends the default one. see bellow.        
        }
    
      }
    

    R.style.colorPickerStyle corresponds to :

    
    

    I simply extend a default Dialog style with my needs.

    Finally, you can invoke this dialog with :

    private void showDialog() {
       ColorDialogFragment newFragment = new ColorDialogFragment();
       newFragment.show(getSupportFragmentManager(), "colorPicker");
       }   
    

提交回复
热议问题