设置DialogFragment的大小

我一直在尝试很多命令来设置我的DialogFragment的大小。 它只包含一个颜色select器,所以我删除了对话框的背景和标题:

 getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable( new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

不过,我也想把对话放在我想要的地方,这是有问题的。 我用:

 WindowManager.LayoutParams params = getDialog().getWindow().getAttributes(); params.width = LayoutParams.WRAP_CONTENT; params.height = LayoutParams.WRAP_CONTENT; params.gravity = Gravity.LEFT; getDialog().getWindow().setAttributes(params); 

但是一个(大的)障碍依然存在:即使我的对话框不可见,它仍然有一定的大小,并且限制了我的对话框的位置。 LayoutParams.WRAP_CONTENT在这里将这个窗格的大小限制到我的颜色select器,但由于某种原因它不起作用。

有没有人能够做类似的事情?

Solutions Collecting From Web of "设置DialogFragment的大小"

我遇到了类似的问题,你不能设置对话框的宽度在代码中的高度,经过多次尝试,我find了解决办法;

这里是自定义DialogFragment的步骤:

1.从方法上的xml中查找自定义视图

 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.在onResume()中设置对话框的宽度为高度,onResume()/ onStart()中的remrember似乎在其他方法中不起作用

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

经过一些试验和错误,我find了解决办法。

这里是我的DialogFragment类的实现:

 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对应于:

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

我只是扩展一个默认的对话框样式与我的需求。

最后,你可以用下面的方法调用这个对话框:

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

对于我的用例,我希望DialogFragment匹配项目列表的大小。 片段视图是名为fragment_sound_picker的布局中的RecyclerView。 我在RecyclerView周围添加了一个包装器RelativeLayout。

我已经使用R.attr.listItemPreferredHeight在名为item_sound_choice的布局中设置了单独的列表项视图的高度。

DialogFragment从膨胀的View的RecyclerView中获取一个LayoutParams实例,将LayoutParams的高度调整为列表长度的倍数,然后将修改的LayoutParams应用到膨胀的父视图中。

其结果是DialogFragment完美包装了select的短名单。 它包括窗口标题和取消/确定button。

这是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); } 

这是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> 

使用此代码调整对话框片段android的大小

 public void onResume() { super.onResume(); super.onResume(); Window window = getDialog().getWindow(); window.setLayout(250, 100); window.setGravity(Gravity.RIGHT); }