gpt4 book ai didi

java - LibGDX Admob LinearLayout 代码

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:55 24 4
gpt4 key购买 nike

我正在尝试将 admob 放入我的 LIBGDX 应用程序中,但是按照 GitHub 教程,我无法运行我的应用程序。请有人尝试帮助我设置代码。下面是main_activity,main.xml。注意:所有修改均在 AndroidManifest.xml 中进行,如 google admob 教程所示。

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

</LinearLayout>

主要 Activity :

 public class MainActivity extends AndroidApplication implements AdsController {

private static final String BANNER_AD_UNIT_ID = "ca-app-pub-3954521267929789/5402418153";

AdView bannerAd;

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

AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;

View gameView = initializeForView(new FXGame(this), cfg);
setupAds();

RelativeLayout layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(bannerAd, params);

setContentView(layout);
}

public void setupAds() {
bannerAd = new AdView(this);
bannerAd.setVisibility(View.INVISIBLE);
bannerAd.setBackgroundColor(0xff000000); // black
bannerAd.setAdUnitId(BANNER_AD_UNIT_ID);
bannerAd.setAdSize(AdSize.SMART_BANNER);
}

@Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
bannerAd.loadAd(ad);
}
});
}

@Override
public void hideBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.INVISIBLE);
}
});
}
}

我的 FXGame:

 public class FXGame extends Game {

private AdsController adsController;

public ZBGame(AdsController adsController){
this.adsController = adsController;
}

@Override
public void create() {
AssetLoader.load();
setScreen(new SplashScreen(this));
adsController.showBannerAd();

}

@Override
public void dispose() {
super.dispose();
AssetLoader.dispose();
}
}

接口(interface)广告 Controller :

public interface AdsController {

public void showBannerAd();
public void hideBannerAd();

}

最佳答案

您在 showBannerAd 和 hideBannerAd 方法中设置的bannerAd可见性错误。应该是相反的。另外,请确保您正在调用 show showBannerAd()。

关于java - LibGDX Admob LinearLayout 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485900/

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