我制作了一款免费的Android游戏,我想为它添加一个横幅。我跟着this tutorial,我不知道为什么它不起作用。
当我在LogCat中的模拟器上运行游戏时,我看到一条消息“广告不可见。不刷新广告。”当我在手机上安装游戏时,我也看不到任何广告。
这是我的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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="hidden"
ads:adSize="BANNER"/>
</LinearLayout>
这是MainActivity.java中的代码
public class MainActivity extends AndroidApplication {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = true;
cfg.useCompass = true;
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
}
@Override
public void onPause() {
adView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
adView.resume();
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
}
答案 0 :(得分:3)
我发现使用adMob
在文件中明确定义布局可能是一个巨大的头痛生成器,因为有时它可能会变得不可预测的一些参数和布局组合。似乎总是有效的是将AdView
动态添加到某些LinearLayout
。
这应该有效:
final LinearLayout fragLayout = (LinearLayout) inflater.inflate(R.layout.your_layout, container, false);
final AdView adView = new AdView(getActivity());
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
// Here you'll append the new AdView
final LinearLayout publiView = (LinearLayout) fragLayout.findViewById(R.id.publi);
publiView.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
// You should include a line like this for testing purposes,
// but only after you've tested whether your AdView works!
// This will prevent your ad being loaded each time you test
// your ad, so it will prevent you being blocked from AdMob.
// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
----编辑----
此实现适用于Fragment
,您可能正在运行Activity
,这就是您收到这些错误的原因。
将getActivity()
替换为this
inflater
简单地(正如其名称所示)从布局文件中膨胀新的LinearLayout
。这意味着我已经定义了一个我称为your_layout
并在LinearLayout
内定义的布局。 inflater只是创建了一个“LinearLayout”的实例。在你的情况下,可能没有必要。
我正在添加可能适合您的代码:
final LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_linearlayout_id_where_you_want_to_put_your_adview);
final AdView adView = new AdView(this);
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
yourLayout.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
// You should include a line like this for testing purposes,
// but only after you've tested whether your AdView works!
// This will prevent your ad being loaded each time you test
// your ad, so it will prevent you being blocked from AdMob.
// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
答案 1 :(得分:1)
要在libgdx中展示广告,您需要初始化应用以供观看。所以,替换所有这些:
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
有了这个:
RelativeLayout layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View gameView = initializeForView(new RedSquare(), cfg);
AdView AdView = new AdView(this);
AdView.setAdSize(AdSize.SMART_BANNER);
AdView.setAdUnitId("***"); //The AdUnitId
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice("***"); //Your Test device if any
AdView.loadAd(adRequest.build());
layout.addView(gameView);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(AdView, adParams);
setContentView(layout);
答案 2 :(得分:0)
您的布局没有什么特别的错误。虽然我可能会将AdView的layout_width更改为:
android:layout_width="match_parent"
但目前尚不清楚
是什么initialize (new RedSquare(), cfg);
正在做什么?我怀疑它正在改变布局。