2013-02-18 104 views
21

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

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在这里将此窗格的大小限制为我的颜色选择器,但由于某种原因,它不起作用。

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

+1

尺寸和对话框的位置需要在Dialog构造函数中设置。您必须重写DialogFragment的onCreateDialog以及您必须创建自定义对话框的位置。 – 2013-02-19 00:15:24

+1

+ Sudar Nimalan谢谢,这是解决方案的一部分。 – Teovald 2013-02-22 16:46:58

+3

对于将来的读者,相关问题: 1)[设置DialogFragment的大小(宽度和高度)](http://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height) 2)[Positioning DialogFragment](http://stackoverflow.com/questions/9698410/position-of-dialogfragment-in-android) – Jonik 2014-02-26 20:20:33

回答

20

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

这里是我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"); 
    } 
+0

非常感谢你!我想问你,你在做什么关于将对话放在你想要的位置?你能做到吗,怎么做? – 2013-04-15 17:23:56

+1

在onCreateView中,您还可以通过以下方式访问布局的参数:WindowManager.LayoutParams wmlp = getDialog()。getWindow()。getAttributes(); 。然后,例如,如果要将对话框放在屏幕的左侧,只需使用wmlp.gravity = Gravity.LEFT;那么wmlp.x = this.getResources()。getDimensionPixelOffset(R.dimen.dialog_position); – Teovald 2013-04-15 21:27:42

+1

真棒@Teovald,为你提供最好的体验。非常感谢你! – 2013-04-15 23:56:01

41

我遇到了类似的问题,那就是你不能设置dialogFragment的宽度在代码中的高度,经过多次尝试,我发现了一个解决方案;

这里是步骤来定制DialogFragment:从XML

1.inflate自定义视图上的方法

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()的高度,remrember中的onResume()/在onStart(),似乎没有其他方法

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

最简单的解决方案! – 2013-11-18 15:02:57

+0

这也适用于我。出于某种原因,我的顶部/底部边距被忽略,但我将其更改为填充并适用于我。 – SilithCrowe 2014-04-07 19:15:03

+0

也为我工作。谢谢 – Munazza 2014-04-10 10:22:12

5

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

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

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

结果是DialogFragment完美包装了选择的短名单。它包括窗口标题和取消/确定按钮。

这里是在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> 
2

使用此代码为对话片段的大小调整的Android

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

     super.onResume(); 
     Window window = getDialog().getWindow(); 
     window.setLayout(250, 100); 
     window.setGravity(Gravity.RIGHT); 
    } 
+0

我认为宽度和高度取决于屏幕大小,但我真的找不到模式,因为我在将对话框片段设置为大小时遇到​​问题。 如果我设置高度150,并且如果获得片段的高度,它会给我其他答案。请帮助我在这个问题https://stackoverflow.com/questions/45494644/how-to-set-the-height-of-the-dialog-fragment-accurately-in-android – 2017-08-03 22:53:49