简体   繁体   English

按钮和GLSurfaceView

[英]Button and GLSurfaceView

I have a GLSurfaceView where I show some animations using OpenGL. 我有一个GLSurfaceView,我在其中使用OpenGL显示一些动画。

I now want to add a button to this view. 我现在想在此视图中添加一个按钮。 How is this accomplished? 这是如何完成的?

Can it be done without involving the xml layout? 可以在不涉及xml布局的情况下完成吗?

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: 在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));

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. 如果你想要屏幕底部的按钮,你可以使用Gravity.BOTTOM而不是Gravity.CENTER_VERTICAL等。

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. 如果您的GLSurfaceView正在拦截触摸,或者您的按钮不会接收触摸事件,请确保在触摸事件方法中调用return super.onTouch ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM