设置DialogFragment的大小 [英] Setting the size of a DialogFragment

查看:226
本文介绍了设置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>

我只是延长默认的对话风格与我的需求。

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆