因此,我使用 Play Services SDK 实现了 AdMob 广告。 我已“按规定”完成所有操作,但广告不会显示。
如果我将 AdView 背景设置为白色,它会显示空白区域而不是广告。
我正在使用 fragment ,但我将 AdView 放在 activity_main.xml
中(尽管在 fragment 中我在底部设置了 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" />
我错过了什么?
最佳答案
我想除了你已经拥有的这个 block 之外:
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 处罚。
关于java - AdMob 广告未展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23848423/