设置 DialogFragment 的大小 [英] Setting the size of a DialogFragment
问题描述
我一直在尝试许多命令来设置我的 DialogFragment
的大小.它只包含一个颜色选择器,所以我删除了对话框的背景和标题:
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 dialog:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
但是,我也想将对话框放置在我想要的位置,但这是有问题的.我用:
However I also want to position the dialog where I want and it is problematic. I use:
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
用于将此窗格的大小限制为我的颜色选择器,但由于某种原因它不起作用.
But one (big) obstacle remains: even though my dialog pane is invisible, it still has a certain size, and it limits the positions of my dialog. The LayoutParams.WRAP_CONTENT
are here to limit the size of this pane to my color-picker, but for some reason it does not work.
有没有人做过类似的事情?
Has anyone been able to do something similar?
推荐答案
经过反复试验,我找到了解决方案.
After some trial and error, I have found the solution.
这是我的 DialogFragment 类的实现:
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 对应:
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>
我只是根据我的需要扩展了默认的 Dialog 样式.
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");
}
这篇关于设置 DialogFragment 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!