AdMob + cocos2d:如何在游戏场景中展示横幅?

时间:2014-03-18 15:39:17

标签: android admob cocos2d-android

我正在尝试基于cocos2d引擎创建我的第一个游戏,但我坚持使用AdMob实现。 横幅显示效果很好,但如果我运行游戏,它会与横幅重叠。 我尝试了很多变种,但没有任何作用。

如何在游戏中显示AdMob横幅? 我将不胜感激。

这是我的MainActivity.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mGLSurfaceView = new CCGLSurfaceView(this);

        setContentView(R.layout.activity_main);

        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("xxxxxxxxxxxxxxxxxx");

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();

        adView.loadAd(adRequest);

        LinearLayout game_layer = (LinearLayout)findViewById(R.id.gameLayout);
        game_layer.addView(mGLSurfaceView);

        LinearLayout banner_layer = (LinearLayout)findViewById(R.id.adLayout);
        banner_layer.addView(adView);
    }

    @Override
    public void onStart() {
        super.onStart();
        CCDirector.sharedDirector().attachInView(mGLSurfaceView);
        CCDirector.sharedDirector().setDisplayFPS(true);
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
        CCScene scene = GameStartLayer.scene();
        CCDirector.sharedDirector().runWithScene(scene);
    }

和activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/linearLayout"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:id="@+id/adLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" 
                android:layout_alignParentBottom="true">
            </LinearLayout>
            <LinearLayout
                android:id="@+id/gameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的问题与您的布局有关。你告诉gameLayout要消耗它父母的所有高度。请尝试使用以下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linearLayout"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:id="@+id/gameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>
        <LinearLayout
            android:id="@+id/adLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </LinearLayout>
</LinearLayout>

layout_weight 会让gameLayout展开以填充剩余空间。

相关问题