1.先添加admob所需要的jar包
2.在所依赖的libcocos2dx工程里面添加main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- <EditText -->
<!-- android:id="@+id/textField" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="wrap_content" -->
<!-- android:background="@null" /> -->
<org.cocos2dx.lib.Cocos2dxGLSurfaceView
android:id="@+id/game_gl_surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="@+id/ADLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
ads:adSize="BANNER"
ads:adUnitId="a14eafbb0936d03"
ads:loadAdOnCreate="true" />
</RelativeLayout>
</FrameLayout>
3.修改Cocos2dxActivity.java
将this.init()----->myInit();
private void myInit()
{
setContentView(R.layout.main);
mGLSurefaceView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);
mGLSurefaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
}
4. 清单文件里面
<!-- ********* Admob ********* -->
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
注意,使用的android sdk版本最好4.0及其以上
然后添加权限:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
即可