开发者

Button and GLSurfaceView

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 don开发者_如何学运维e without involving the xml layout?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜