DialogFragment在Android的位置位置、DialogFragment、Android

2023-09-12 07:01:46 作者:救星。

我有一个 DialogFragment 来显示查看就像一个弹出式屏幕。 窗口总是出现在屏幕的中间。 有没有一种方法来设置 DialogFragment 窗口的位置? 我特地到source code 也没有找到任何东西。

I have a DialogFragment to show a View like a Popup screen. The Window appears always in the middle of the screen. Is there a way to set the position of the DialogFragment window? I have looked in to the source code but couldn't find anything yet.

推荐答案

尝试是这样的:

@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()

要在调用设置的内容后设置的位置。

be sure to set the position after calling set-content.