不在 android 中的可绘制对象 canvas 中显示横幅广告

Not display banner ad in the drawable canvas in android

我通过 android 中的 drawable canvas 实现了游戏,我想要横幅广告但无法显示。这是代码:-

GameActivity.java(此 GameActivity.java 已连接到我的 gameview.java 文件,我在其中借助可绘制对象 canvas 使用所有游戏代码)

public class GameActivity extends Activity
{
    private AdView mAdView;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {

    super.onCreate(savedInstanceState);
    //GameView gView = new GameView(this);
    GameView gView = new GameView(this);
     gView.setKeepScreenOn(true);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(
     WindowManager.LayoutParams.FLAG_FULLSCREEN,
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
     MobileAds.initialize(getApplicationContext(), "ca-app-pub-0664573200302260/332653322424");
     LinearLayout layout = new LinearLayout(this);
     layout.setOrientation(LinearLayout.VERTICAL);

//Additionally to adjust the position to Bottom
     layout.setGravity(Gravity.BOTTOM);

// Create a banner ad
     mAdView = new AdView(this);
     mAdView.setVisibility(View.VISIBLE);
     mAdView.setAdSize(AdSize.BANNER);
     mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

// Create an ad request.
     AdRequest.Builder adRequestBuilder = new AdRequest.Builder();


// Optionally populate the ad request builder.
     adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

// Add the AdView to the view hierarchy.
     layout.addView(mAdView);

// Start loading the ad.
     mAdView.loadAd(adRequestBuilder.build());



     setContentView(gView);
 }
}

您将内容视图设置为 GameView。改为将其设置为布局:

setContentView(layout);

GameView 是单一视图,而布局包含 GameView 和 AdView