Setting the size of a DialogFragment

谁都会走 提交于 2019-11-28 04:23:22

i met a similar question that is you can't set the dialogFragment's width an height in code,after several try ,i found a solution;

here is steps to custom DialogFragment:

1.inflate custom view from xml on method

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getDialog().setCanceledOnTouchOutside(true);

    View view = inflater.inflate(R.layout.XXX,
            container, false);
    //TODO:findViewById, etc
    return view;
}

2.set your dialog's width an height in onResume(),remrember in onResume()/onStart(),seems didn't work in other method

public void onResume()
{
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(width, height);
    window.setGravity(Gravity.CENTER);
    //TODO:
}  

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 :

<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:cacheColorHint">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

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");
   }   

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<SoundItem> 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:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_sound_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
Mahi Saini

use this code for resize of Dialog Fragment android

public void onResume() {
        super.onResume();

        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(250, 100);
        window.setGravity(Gravity.RIGHT);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!