Setting the size of a DialogFragment

前端 未结 4 1607
执笔经年
执笔经年 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:40

    For my use case, I wanted the DialogFragment to match the size of a list of items. The fragment view is a RecyclerView in a layout called fragment_sound_picker. I added a wrapper RelativeLayout around the RecyclerView.

    I had already set the individual list item view's height with R.attr.listItemPreferredHeight, in a layout called item_sound_choice.

    The DialogFragment obtains a LayoutParams instance from the inflated View's RecyclerView, tweaks the LayoutParams height to a multiple of the list length, and applies the modified LayoutParams to the inflated parent View.

    The result is that the DialogFragment perfectly wraps the short list of choices. It includes the window title and Cancel/OK buttons.

    Here's the setup in the DialogFragment:

    // SoundPicker.java
    // extends DialogFragment
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(getActivity().getString(R.string.txt_sound_picker_dialog_title));
    
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater.inflate(R.layout.fragment_sound_picker, null);
        RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_sound_list);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
    
        SoundPickerAdapter soundPickerAdapter = new SoundPickerAdapter(getActivity().getApplicationContext(), this, selectedSound);
        List items = getArguments().getParcelableArrayList(SOUND_ITEMS);
        soundPickerAdapter.setSoundItems(items);
        soundPickerAdapter.setRecyclerView(rv);
        rv.setAdapter(soundPickerAdapter);
    
        // Here's the LayoutParams setup
        ViewGroup.LayoutParams layoutParams = rv.getLayoutParams();
        layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = getListItemHeight() * (items.size() + 1);
        view.setLayoutParams(layoutParams);
    
        builder.setView(view);
        builder.setCancelable(true);
    
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            // ...
        });
    
        builder.setPositiveButton(R.string.txt_ok, new DialogInterface.OnClickListener() {
            // ...
        });
    
        return builder.create();
    }
    
    @Override
    public void onResume() {
        Window window = getDialog().getWindow();
        window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        super.onResume();
    }
    
    private int getListItemHeight() {
        TypedValue typedValue = new TypedValue();
        getActivity().getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true);
        DisplayMetrics metrics = new android.util.DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return (int) typedValue.getDimension(metrics);
    }
    

    Here is fragment_sound_picker:

    
    
    
        
    
    

提交回复
热议问题