【问题标题】:Setting the size of a DialogFragment设置 DialogFragment 的大小
【发布时间】:2013-02-18 23:01:52
【问题描述】:

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

有没有人做过类似的事情?

【问题讨论】:

标签: android android-dialogfragment


【解决方案1】:

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

这里是自定义 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:
}  

【讨论】:

  • 这对我也有用。由于某种原因,我的顶部/底部边距被忽略了,但我将其更改为填充,它对我有用。
  • onResume() 中的代码也可以在 onCreateDialog() 中使用,供那些已经在使用该方法进行各种其他对话黑客攻击的人使用。
  • 效果很好。它无需 onCreateView 中的任何自定义即可工作。仅 onResume() 中的 Window 内容就可以了。
  • 真是天才。继续努力。只是出于好奇想知道为什么这个问题出现在少数设备上而不是其他设备上?
  • 我认为宽度和高度取决于屏幕尺寸,但我无法真正找到模式,因为在将尺寸设置为对话框片段时我遇到了问题。如果我将高度设置为 150,并且如果获取片段的高度,它会给我其他答案。请帮助我解决这个问题stackoverflow.com/questions/45494644/…
【解决方案2】:

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

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

【讨论】:

  • 非常感谢!我想问你你是怎么把对话框定位在你想要的位置的?你能做到吗?怎么做?
  • 在onCreateView中,也可以通过以下方式访问布局的参数: WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes(); .然后例如如果你想把对话框放在屏幕的左侧,你只需要使用 wmlp.gravity = Gravity.LEFT;然后 wmlp.x = this.getResources().getDimensionPixelOffset(R.dimen.dialog_position);
  • ... 除了这如何让您设置对话框的宽度?
【解决方案3】:

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

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

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>

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    • 我认为宽度和高度取决于屏幕尺寸,但我无法真正找到模式,因为在将尺寸设置为对话框片段时我遇到了问题。如果我将高度设置为 150,并且如果获取片段的高度,它会给我其他答案。请帮助我解决这个问题stackoverflow.com/questions/45494644/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多