按钮和 GLSurfaceView

发布于 2024-09-16 11:09:22 字数 102 浏览 0 评论 0原文

我有一个 GLSurfaceView,我在其中使用 OpenGL 显示一些动画。

我现在想向该视图添加一个按钮。这是如何实现的?

不涉及xml布局可以做到吗?

I have a GLSurfaceView where I show some animations using OpenGL.

I now want to add a button to this view. How is this accomplished?

Can it be done without involving the xml layout?

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

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

发布评论

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

评论(1

友欢 2024-09-23 11:09:22

您可以手动构建视图并将其添加到活动的内容视图中。在 GLSurfaceView 上执行 setContentView 或通过 XML 布局后,在 Activity 的 onCreate 方法中,您可以执行以下操作,这将在左上角的 GLSurfaceView 顶部添加一个按钮:

 Button b = new Button(this);
 b.setText("Hello World");
 this.addContentView(b,
            new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

如果您希望按钮位于屏幕上的其他位置您需要将其添加到布局中,然后将该布局添加到内容视图中。要让按钮位于屏幕中央,您可以执行以下操作:

LinearLayout ll = new LinearLayout(this);
Button b = new Button(this);
b.setText("hello world");
ll.addView(b);
ll.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
this.addContentView(ll,
            new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

如果您希望按钮位于屏幕底部,您可以使用 Gravity.BOTTOM 而不是 Gravity.CENTER_VERTICAL 等。

确保您正在调用 return super。 onTouch... 在您的触摸事件方法中,如果您的 GLSurfaceView 正在拦截触摸,否则您的按钮将不会接收触摸事件。

You can manually build and add Views to the content view of the Activity. In the onCreate method in your Activity after doing setContentView on your GLSurfaceView or through an XML layout you can do the following which will add a button on top of the GLSurfaceView in the upper left corner:

 Button b = new Button(this);
 b.setText("Hello World");
 this.addContentView(b,
            new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

If you want the button to be somewhere else on screen you will need to add it to a layout and then add that layout to the content view. To have a button that is in the center of the screen you can do the following:

LinearLayout ll = new LinearLayout(this);
Button b = new Button(this);
b.setText("hello world");
ll.addView(b);
ll.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
this.addContentView(ll,
            new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

If you want the button on the bottom of the screen you can use Gravity.BOTTOM instead of Gravity.CENTER_VERTICAL etc.

Make sure you are calling return super.onTouch... in your touch event methods if your GLSurfaceView is intercepting touches or else your button will not receive touch events.

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