我有一个 DialogFragment 要显示一个 View 像弹出屏幕。窗口始终显示在屏幕中间。有没有办法设置的位置 DialogFragment 窗户?我已经看到了 源代码 但还找不到任何东西。

有帮助吗?

解决方案

尝试这样的事情:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
    WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
    p.width = ViewGroup.LayoutParams.MATCH_PARENT;
    p.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
    p.x = 200;
    ...
    getDialog().getWindow().setAttributes(p);
    ...

或其他方法用于 getDialog().getWindow().

请务必在调用set-content后设置位置。

其他提示

对,我用这个头撞了墙一两个小时,最后才得到 DialogFragment 就像我想要的那样。

我在继续前进 Steelight的答案 这儿。这是我发现的最简单,最可靠的方法。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
    Window window = getDialog().getWindow();

    // set "origin" to top left corner, so to speak
    window.setGravity(Gravity.TOP|Gravity.LEFT);

    // after that, setting values for x and y works "naturally"
    WindowManager.LayoutParams params = window.getAttributes();
    params.x = 300;
    params.y = 100;
    window.setAttributes(params);

    Log.d(TAG, String.format("Positioning DialogFragment to: x %d; y %d", params.x, params.y));
} 

请注意 params.widthparams.softInputMode (在Steelight的答案中使用)对此无关紧要。


下面是一个更完整的例子。我真正需要的是将"确认框"DialogFragment对齐到"源"或"父"视图旁边,在我的情况下是ImageButton。

我选择使用DialogFragment,而不是任何自定义片段,因为它为您提供了免费的"对话框"功能(当用户点击它之外时关闭对话框等)。

Example ConfirmBox
示例确认框上面的"源"ImageButton(trashcan)

/**
 * A custom DialogFragment that is positioned above given "source" component.
 *
 * @author Jonik, https://stackoverflow.com/a/20419231/56285
 */
public class ConfirmBox extends DialogFragment {
    private View source;

    public ConfirmBox() {
    }

    public ConfirmBox(View source) {
        this.source = source;            
    }

    public static ConfirmBox newInstance(View source) {
        return new ConfirmBox(source);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(STYLE_NO_FRAME, R.style.Dialog);
    }


    @Override
    public void onStart() {
        super.onStart();

        // Less dimmed background; see https://stackoverflow.com/q/13822842/56285
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount = 0.2f; // dim only a little bit
        window.setAttributes(params);

        // Transparent background; see https://stackoverflow.com/q/15007272/56285
        // (Needed to make dialog's alpha shadow look good)
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Put your dialog layout in R.layout.view_confirm_box
        View view = inflater.inflate(R.layout.view_confirm_box, container, false);

        // Initialise what you need; set e.g. button texts and listeners, etc.

        // ...

        setDialogPosition();

        return view;
    }

    /**
     * Try to position this dialog next to "source" view
     */
    private void setDialogPosition() {
        if (source == null) {
            return; // Leave the dialog in default position
        }

        // Find out location of source component on screen
        // see https://stackoverflow.com/a/6798093/56285
        int[] location = new int[2];
        source.getLocationOnScreen(location);
        int sourceX = location[0];
        int sourceY = location[1];

        Window window = getDialog().getWindow();

        // set "origin" to top left corner
        window.setGravity(Gravity.TOP|Gravity.LEFT);

        WindowManager.LayoutParams params = window.getAttributes();

        // Just an example; edit to suit your needs.
        params.x = sourceX - dpToPx(110); // about half of confirm button size left of source view
        params.y = sourceY - dpToPx(80); // above source view

        window.setAttributes(params);
    }

    public int dpToPx(float valueInDp) {
        DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }
}

通过根据需要添加构造函数参数或设置器,可以很容易地使上述更通用。(我的决赛 ConfirmBox 有一个样式按钮(在一些边框内等),其文本和 View.OnClickListener 可以在代码中定制。)

getDialog().getWindow() 不会为一个工作 DialogFragment 作为 getWindow() 如果托管活动不可见,则返回null,如果您正在编写基于片段的应用程序,则不会返回null。你会得到一个 NullPointerException 当你尝试 getAttributes().

我推荐Mobistry的答案。如果你已经有一个DialogFragment类,切换过来并不太难。只需将onCreateDialog方法替换为构造并返回PopupWindow的方法即可。然后,您应该能够重用您提供给的视图 AlertDialog.builder.setView(), ,并调用 (PopupWindow object).showAtLocation().

您需要在与以下内容中覆盖对话框中的onresume()方法:

@Override
public void onResume() {
    final Window dialogWindow = getDialog().getWindow();
    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
    lp.x = 100;        // set your X position here
    lp.y = 200;        // set your Y position here
    dialogWindow.setAttributes(lp);

    super.onResume();
}
.

我在此目的使用popupwindow类,因为您可以指定视图的位置和大小。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new Dialog(mActivity, R.style.BottomDialog);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 
    dialog.setContentView(R.layout.layout_video_live);
    dialog.setCanceledOnTouchOutside(true);

    Window window = dialog.getWindow();
    assert window != null;
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.gravity = Gravity.BOTTOM; //psotion
    lp.width = WindowManager.LayoutParams.MATCH_PARENT; // fuill screen
    lp.height = 280;
    window.setAttributes(lp);
    return dialog;
}
.

我正在使用 AppCompatDialogFragmentandroid.support.v7.app.AppCompatDialogFragment 我想将对话框片段对齐到屏幕底部,并删除所有边框,特别是我需要设置内容宽度以匹配父级。

所以,我想从这个(黄色背景来自对话框片段的rootLayout):

src_img_1

拿着这个:

src_img_2

没有一个解决方案奏效。所以,我设法做到了这一点:

fun AppCompatDialogFragment.alignToBottom() {
    dialog.window.apply {
        setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL)
        decorView.apply {

            // Get screen width
            val displayMetrics = DisplayMetrics().apply {
                windowManager.defaultDisplay.getMetrics(this)
            }

            setBackgroundColor(Color.WHITE) // I don't know why it is required, without it background of rootView is ignored (is transparent even if set in xml/runtime)
            minimumWidth = displayMetrics.widthPixels
            setPadding(0, 0, 0, 0)
            layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
            invalidate()
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top