Reprint please indicate source: http://blog.csdn.net/vistatns/article/details/51316462
(porting the Cocos2d-x project to Android please go to : http://blog.csdn.net/vistatns/article/details/51316103)
1. Add a layout file main.xml
<? XML version="1.0" encoding="Utf-8"?>
< Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"
android:layout_width="Match_parent"
android:layout_height="Match_parent"
android:orientation="vertical" >
< Relativelayout
android:id="@+id/gameviewlay"
android:layout_width="Fill_parent"
android:layout_height="fill_parent" />
</ Relativelayout >
2. Introduce the XML layout file into the java file and add the setcocosview , Introduction of XML initialization
@Override
Public void Setcocosview (Cocos2dxglsurfaceview Mglsurfaceview) {
Setcontentview (r.layout. Main);
Relativelayout framelayout = (relativelayout) Findviewbyid (r.id. Gameviewlay);
Framelayout.addview (Mglsurfaceview);
System. out. println ("txc:setcocosview--initialization");
}
3. This time may be error, because there is no such parent method to cover you
(1). Select the first item
Create setcocosview () in coco2dactivity
(2). Move the contents of init () to Setcocosview ()
(3). In Init (), Add the following :
Cocos2dxglsurfaceview
this. Mglsurfaceview =this. Oncreateview ();
//Switch to supported OpenGL (ARGB888) mode on emulator
//if (Isandroidemulator ())
//This.mGLSurfaceView.setEGLConfigChooser (8, 8, 8, 8, +, 0);
this. Mglsurfaceview. Setcocos2dxrenderer (new cocos2dxrenderer ());
Setcocosview (mglsurfaceview);
(4). and look at the original . Java file, no errors.
4. Run the test
It worked!
Add an XML layout file for a cocos2d-x project ported to the Android platform