因此,我使用Play Services SDK实施了AdMob广告。我已经“按书完成”所有操作,但广告不会展示。
如果我将AdView背景设置为白色,则会显示空白,但不会显示广告。我正在使用Fragments,但我将AdView放在了activity_main.xml
(尽管在片段中,我在底部设置了60dp的边距)
这是我的onCreate
代码(包含实际代码和“测试”代码)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
drawer.openDrawer(Gravity.LEFT);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("Device_ID")
.build();
adView.loadAd(adRequest);
}
这是我的 activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.MainActivity">
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adView" >
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="Unit_ID"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
我想念什么?
我认为除了这个块之外,您还具有:
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("Device_ID")
.build();
...您缺少以下内容:
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(adRequest);
这是完整的图片:
xml文件中的内容:
<com.google.android.gms.ads.AdView android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="your ad unit id"
ads:adSize="BANNER"/>
onCreate中的内容:
AdRequest adRequest = new AdRequest.Builder()
.build();
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(adRequest);
请注意,上面的代码用于生产。您必须使用测试设备ID进行测试,以免受到AdMob的处罚。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句