26

我一直在尝试许多命令来设置我的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是为了将此窗格的大小限制为我的颜色选择器,但由于某种原因它不起作用。

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

4

4 回答 4

47

我遇到了一个类似的问题,你不能在代码中设置对话框片段的宽度和高度,经过几次尝试,我找到了解决方案;

这是自定义 DialogFragment 的步骤:

1.inflate自定义视图从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() 中,似乎在其他方法中不起作用

public void onResume()
{
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(width, height);
    window.setGravity(Gravity.CENTER);
    //TODO:
}  
于 2013-07-29T05:11:00.923 回答
22

经过反复试验,我找到了解决方案。

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

我只是根据自己的需要扩展了默认的 Dialog 样式。

最后,您可以使用以下命令调用此对话框:

private void showDialog() {
   ColorDialogFragment newFragment = new ColorDialogFragment();
   newFragment.show(getSupportFragmentManager(), "colorPicker");
   }   
于 2013-02-22T16:56:52.850 回答
5

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

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

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

结果是 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>
于 2015-02-19T02:05:39.877 回答
3

使用此代码调整 Dialog Fragment android 的大小

public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(250, 100);
        window.setGravity(Gravity.RIGHT);
    }
于 2015-10-23T06:20:45.237 回答