DialogFragment 在 Android 中的位置 [英] Position of DialogFragment in Android

查看:68
本文介绍了DialogFragment 在 Android 中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DialogFragment 来显示一个 View 就像一个弹出屏幕.窗口总是出现在屏幕中间.有没有办法设置 DialogFragment 窗口的位置?我查看了 源代码,但还没有找到任何东西.

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() 的其他方法.

一定要在调用set-content后设置位置.

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

这篇关于DialogFragment 在 Android 中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆