首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓PopUpWindow

安卓PopUpWindow
EN

Stack Overflow用户
提问于 2014-12-03 00:22:28
回答 2查看 730关注 0票数 1

我有一个应用程序,它有一个活动,创建一个表面视图和内部的表面视图放置一个OpenGL渲染器。我正在处理OpenGL渲染器中的对象拾取。我想要做的是,当用户选择一个特定的对象时,显示我存储在文件中的文本块和图像。看起来似乎安卓的PopupWindow类可以很好地做到这一点。我可以将弹出窗口覆盖在OpenGL渲染器上吗?还是说我是在倒退?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2014-12-12 00:11:56

我的朋友,我和你也有同样的问题。我将解释我做了什么,我认为它适合你的问题。

我有一个渲染器,我在其中显示一个网格。用户可以在网格上放置注释。我也像你一样做光线拾取。当用户单击评论时,将显示一个弹出窗口。

本质上,您需要做的是在渲染器中调用一个方法,该方法将在UI线程中运行弹出窗口。(OpenGl在自己的线程上运行)。

更具体地说,您必须向将显示弹出窗口的处理程序发送一条消息

YourRenderer.java

代码语言:javascript
复制
@Override
public void onDrawFrame(GL10 gl) {
    .... code that displays the mesh ...

    if(userRequestedPopup) {
        Message message = new Message();
        message.what = DialogHandler.OPEN_ANNOTATION;

        // Here you can create a Bundle in order to pass more data inside the Message

        dialogHanlder.sendMessage(message);

        userRequestedPopup = false;
    }
....
}

YourGLSurfaceView.java您应该在此处创建处理程序

代码语言:javascript
复制
public class DialogHandler extends Handler {
    ... other constants ...
    public static final int OPEN_ANNOTATION = 2;
    ...

    @Override
    public void handleMessage(Message msg) {
        if (msg.what == OPEN_ANNOTATION) {
            new YourPopup(context, msg);
        } 
    }
}

在创建弹出窗口之前,您应该在xml中设计自己的布局(如果需要,也可以通过编程方式创建)。为了理解代码,我应该提到我把我的布局称为popup_layout。此外,您应该添加一个空布局(它应该在宽度和高度上都填充父布局),比如说一个LinearLayout,它将保存弹出窗口。我管它叫popupHolder

YourPopup.java

代码语言:javascript
复制
public Popup(Context context, Message message) {
    this.context = context;
    Activity parent = (Activity) context;

    // here you can have the instance of the holder from glSurfaceView
    LinearLayout parentLayout = (LinearLayout) parent.findViewById(R.id.popupHolder);

    // inflate your custom popup layout or create it dynamically 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view =  inflater.inflate(R.layout.popup_layout, null);

    // Now you should create a PopupWindow
    PopupWindow popupWindow = new PopupWindow(view, 
               LayoutParams.WRAP_CONTENT,  
               LayoutParams.WRAP_CONTENT);
    // you can change the offset values or the Gravity. This will diaply the popup in the center
    // of your glSurfaceView
    popupWindow.showAtLocation(parentLayout, Gravity.CENTER, 0, 0);
}

我认为这就是您所需要的,因为这个解决方案对我来说很好。

希望我能帮上忙。

票数 2
EN

Stack Overflow用户

发布于 2015-03-20 19:40:25

为弹出窗口创建一个XML,然后在Java文件中使用以下代码:

代码语言:javascript
复制
public void popupWindow() {
    PopupWindow pw;
    LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.txtPusdNotR));

    pw = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
    pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

    // Get your Buttons and other tags here

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27254126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档