我尝试在页面底部设置 AdMob 横幅,但广告没有显示。 空格出现在页面底部那里,但 AdMob 不在那里显示。
- 添加和引用 Google Play 服务库。 - 是的
- 在 AndroidManifest.xml 中添加元数据标记。 - 是的
- 在 list 中声明 com.google.android.gms.ads.AdActivity。 - 是的
- 在 list 中设置网络权限。 - 是的
有人告诉我为什么。 谢谢。
我的 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/flContainer" <!-- DATA Layout -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ad_layout"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ad_layout" <!-- AdMob Layout -->
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" />
</RelativeLayout>
主 Activity .java
// Create an ad.
adView = new AdView(context);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view
// will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);
//layout.getBottom();
layout.addView(adView);
// Create an ad request. Check logcat output for the
// hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE").build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
最佳答案
您告诉 fldContainer 来匹配 Parent。这意味着它将占用父级的所有空间,而不会为您的广告留下任何空间。
改为尝试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/flContainer" <!-- DATA Layout -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ad_layout" <!-- AdMob Layout -->
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" />
</LinearLayout>
关于android - 设置页面底部的 AdMob 横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23970747/