gpt4 book ai didi

java - 在 Android 游戏的 Android Surface View 上集成 AdMob 横幅广告

转载 作者:行者123 更新时间:2023-11-29 02:28:24 25 4
gpt4 key购买 nike

我正在开发一款安卓游戏。上下文 View 扩展了表面 View 类。我试图在屏幕底部放置一个横幅。我已经使用 this guide 设置了 Google AdMob SDK .我已将 AdView 添加到布局并使用 another guide 加载测试广告.

这是我的 MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));

MobileAds.initialize(this, "ca-app-pub-1042328490971088~5397056328");

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}

当我运行这段代码时,在我的游戏继续正常运行的同时根本没有显示任何广告。我的想法是,也许广告在那里但不可见,因为游戏 View 占据了整个屏幕。如果有人可以帮助我确保我的横幅广告覆盖在 Canvas 上或缩小 Canvas 以容纳广告,我将不胜感激。

还有我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.haakamaujla.myfirstgame">

<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="Stunty Skys"
android:roundIcon="@drawable/icon"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

这是我的 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

最佳答案

在搜索帖子后,我找到了问题的答案:

adView.setBackgroundColor(Color.TRANSPARENT);

显然,这会强制显示广告,否则不会一直显示。

关于java - 在 Android 游戏的 Android Surface View 上集成 AdMob 横幅广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50919419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com