在GLSurfaceView上添加Layout控件(android)

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

源码:https://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) context
3 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
4 LinearLayout imagebuttonLinearLayout = (LinearLayout) inflater
5 .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、因特网***

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


posted @ 2011-10-13 16:48  maadiah  阅读(8746)  评论(1编辑  收藏  举报