1

我正在尝试制作一个弹出窗口,该窗口会在我的应用程序中单击按钮时显示一个由适配器填充的列表视图。目前,我正在使用 DialogFragment 并且它工作正常。但我希望能够调整对话框的大小和理想的位置。

在做了一些研究之后,PopupWindow 允许您调整框的大小和位置,但它在其视图中实例化片段时效果不佳。DialogFragment 非常适合使用片段和列表视图,但据我所知,您无法控制它的大小和位置。

有没有人在创建动态弹出窗口方面有类似的情况或经验?

4

1 回答 1

2

要更改 DialogFragment 的大小,我使用 OnCreateView 方法执行了以下操作

注意: MonoDroid 的 C# 代码;为原始 Android 轻松翻译成 Java

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreateView(inflater, container, savedInstanceState);

    View thisView = inflater.Inflate(Resource.Layout.NewContactDialog, container, false);

    thisView.Post(() =>
        {
            ViewGroup.LayoutParams lp = thisView.LayoutParameters;

            lp.Width = 500;

            thisView.LayoutParameters = lp;

            thisView.Invalidate();
        }

    );

    return thisView;
}

此代码来自实验。我决定的只是使用居中的 DialogFragment (默认行为)并取消背景。您可以覆盖 DF 的样式,并将其基于现有主题

<style name="MyDialogFragment" parent="android:Theme.Holo.Dialog">
  <item name="android:windowIsFloating">true</item>
</style>

或者从头开始完全设计它

<style name="MyDialogFragment">
  ... (set all items, check docs)
</style>

对于开始尝试使用上面的 MyDialogFragment 样式并将 windowIsFloating 设置为 false 并查看效果(只需使用 SetStyle 成员将您的主题设置为此)。

一些很好的样式和主题链接,您需要自己设置样式并覆盖默认值:

http://www.therealjoshua.com/2012/01/styling-android-with-defaults/

http://developer.android.com/guide/topics/ui/themes.html/

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml/

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml/

老实说,使用 DialogFragments 和 PopupWindows 很痛苦,但它们是解决一些实际 GUI 问题的专用视图,所以不要放弃。如果您需要更多信息,请在下面的评论中提问(我意识到这不是答案,我只是想提供帮助,b/c 我现在正在研究这些东西)。

于 2012-12-11T21:27:45.037 回答