如何在 Android 中使非 XML PopupWindow 出现在屏幕上

发布于 2024-12-16 17:33:20 字数 1018 浏览 2 评论 0原文

我创建了一个类,它根据输入的参数生成 PopupWindow。我相信这比为 PopupWindow 操作基于 XML 的内容更能满足我的需求。

创建窗口及其内容似乎进行得很顺利 - 实际上是让该内容显示在我还无法管理的屏幕上。问题是我无法找到正在使用的 PopupWindow 代码示例,该示例不依赖 LayoutInflater 函数将其放置在屏幕上。由于我的 PopupWindow 不是从 XML 文件生成的,因此我无法使用 LayoutInflater 将其放置在屏幕上。

我可能应该解释的其他事情是我的 PopupWindow 生成类在它自己的文件中。即它不是活动文件的子类。我已经这样做了,这样我就可以轻松地将我的自定义 PopupWindow 类复制到我将来可能开发的任何项目中。

这是我的类的基本布局:

class myPopup extends Object {

    public myPopup(parameters){
        ViewGroup winBody;
        // "winbBody" will be the content of the PopupWindow.
        // Code that fills and adjusts "winBody" based on the parameters goes here.

        int width = //Determined by parameters.
        int height = //Determined by parematers.
        PopupWindow pw = new PopupWindow(winBody, width, height, true);
        //This is as far as I seem to get before getting stuck.
    }

}

我认为我应该以某种方式使用 PopupWindow 函数“showAtLocation”,但我不清楚我应该为此使用哪些参数。有人可以告诉我如何让弹出窗口出现在屏幕上吗?希望在它的正中心。 :)

I've created a class that generates a PopupWindow based on parameters fed into it. I believe this will suit my needs better than manipulating XML-based content for my PopupWindow.

Creating the window and it's content seems to go through smoothly - it's actually getting that content to appear on the screen that I haven't been able to manage yet. The problem is that I haven't been able to find an example of PopupWindow code in use that doesn't rely on the LayoutInflater function to place it on the screen. As my PopupWindow isn't generated from an XML file, I can't use LayoutInflater to place it on the screen.

Something else I should probably explain is that my PopupWindow-generating class is in it's own file. i.e. It is not a subclass of an Activity file. I've done it this way so that I can easilly copy my custom-PopupWindow class to any future projects I might develop.

Here is the basic layout of my class:

class myPopup extends Object {

    public myPopup(parameters){
        ViewGroup winBody;
        // "winbBody" will be the content of the PopupWindow.
        // Code that fills and adjusts "winBody" based on the parameters goes here.

        int width = //Determined by parameters.
        int height = //Determined by parematers.
        PopupWindow pw = new PopupWindow(winBody, width, height, true);
        //This is as far as I seem to get before getting stuck.
    }

}

I gather that I am supposed to somehow use the PopupWindow function "showAtLocation", but I am unclear what parameters I am supposed to use for this. Could someone please tell me how to get my popup window to appear on the screen? Hopefully in the very center of it. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-12-23 17:33:20

尝试类似的方法,

您可以将整个弹出窗口定义为一个活动,然后使其背景消失,这样它看起来就像一个弹出窗口:

<style name="MyTransparentPopup">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

public class PopupWindowActivity extends Activity {

    PopupWindow popUp;
    LinearLayout layout;
    TextView tv;
    LayoutParams params;
    LinearLayout mainLayout;
    Button but;
    boolean click = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        popUp = new PopupWindow(this);
        layout = new LinearLayout(this);
        mainLayout = new LinearLayout(this);
        tv = new TextView(this);
        but = new Button(this);
        but.setText("Show popup");
        but.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (click) {
                    popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
                    popUp.update(0, 0, 300, 80);
                    click = false;
                } else {
                    popUp.dismiss();
                    click = true;
                }
            }

        });
        params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        tv.setText("Hello popup");
        layout.addView(tv, params);
        popUp.setContentView(layout);

        mainLayout.addView(but, params);
        setContentView(mainLayout);
    }
    }

Try something like that

you may define the whole popup window as an activity and just make its background disapear, so it will look like a popup window:

<style name="MyTransparentPopup">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

public class PopupWindowActivity extends Activity {

    PopupWindow popUp;
    LinearLayout layout;
    TextView tv;
    LayoutParams params;
    LinearLayout mainLayout;
    Button but;
    boolean click = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        popUp = new PopupWindow(this);
        layout = new LinearLayout(this);
        mainLayout = new LinearLayout(this);
        tv = new TextView(this);
        but = new Button(this);
        but.setText("Show popup");
        but.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if (click) {
                    popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
                    popUp.update(0, 0, 300, 80);
                    click = false;
                } else {
                    popUp.dismiss();
                    click = true;
                }
            }

        });
        params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        tv.setText("Hello popup");
        layout.addView(tv, params);
        popUp.setContentView(layout);

        mainLayout.addView(but, params);
        setContentView(mainLayout);
    }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文