我試圖把admob放在我的LIBGDX應用程序中,但是按照教程GitHub,我無法運行我的應用程序。請有人試圖幫助我設置代碼。下面是main_activity,main.xml。 注意:所有修改都是在AndroidManifest.xml中作爲教程google admob進行的。LibGDX Admob LinearLayout代碼
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>
MainAcitivity:
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);
}
});
}
}
MY 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();
}
}
接口AdsController:
public interface AdsController {
public void showBannerAd();
public void hideBannerAd();
}
您沒有做任何事情來顯示橫幅或插頁式廣告。您可以按照本系列文章中解釋的步驟進行操作:http://www.norakomi.com/tutorial_admob_part2_banner_ads1.php 嘗試了一些東西,如果仍然無法將admob與libgdx項目集成,我可以幫助您。 –
我已經完成了本教程,但沒有顯示橫幅。本教程在我的代碼中沒有任何錯誤,只是在項目核心中的AdsController中。如果你能幫助我,它會看起來很棒!哈哈 – MTR
你能展示你的代碼來顯示橫幅廣告嗎? –