在GLSurfaceView上添加Layout控件(android)

心已入冬 提交于 2020-01-11 10:22:23

查找了很久,才找出在GLSurfaceView上添加控件的方法。废话不说,本例实现了一个Native opengl es 程序,绘制了一个旋转三角形;当然主题是在GLSurfaceView上添加Layout控件。主要添加了SlidingDrawer和ImageButton。

源码:http://files.cnblogs.com/maadiah/GLSurfaceView_Overlay.zip 你可以自己修改、把玩。

底部四个是ImageButton,右边是弹出的SlidingDrawer(很不好意思,手头没有像样的图片编辑器,简单点,用内置的icon.png)。

 

主要注意这几点:

1.GLSurfaceView要开启xml导入插件功能,所以用GLSurfaceView(Context context, AttributeSet attrs)构造函数。(本例是自己实现的GLSurfaceView

  我是这样获得AttributeSet参数的:

1 Resources res = this.getResources();2         XmlPullParser parser = res.getXml(R.layout.glsurface_overlay);3         AttributeSet attributes = Xml.asAttributeSet(parser);


2.使用context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法来获取XML布局文件。

  代码如下:

1 // add imagebuttons 2         LayoutInflater inflater = (LayoutInflater) context3         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);4 LinearLayout imagebuttonLinearLayout = (LinearLayout) inflater5         .inflate(R.layout.imagebutton, imageButtonLinearLayout, false);6 imageButtonLinearLayout.addView(imagebuttonLinearLayout);


3.通过addContentView来添加XML布局的控件

  代码如下:

1 addContentView(imageButtonLinearLayout, new LayoutParams(LayoutParams.MATCH_PARENT,2                 LayoutParams.MATCH_PARENT));



***参考资料:Google、因特网***

***第一篇技术博文,大家都来写博客,技术在分享、传播中产生价值***


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!