我正在尝试在我的活动中展示AdMob个广告,但它总是会给出“没有足够空间展示广告”的错误。
我的XML文件是:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.google.ads.AdView
android:id="@+id/adMobadView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
ads:adSize="BANNER"
ads:adUnitId="@string/admobid"
android:layout_alignParentBottom="true"
android:gravity="bottom|center_horizontal"
ads:loadAdOnCreate="true" />
</RelativeLayout>
我的Java文件:
AdView ad=(AdView)findViewById(R.id.adMobadView);
AdRequest re = new AdRequest();
re.setTesting(true);
re.setGender(AdRequest.Gender.FEMALE);
ad.loadAd(re);
02-18 14:16:17.869: W/webcore(813): Can't get the viewWidth after the first layout
02-18 14:16:17.979: I/Ads(813): onReceiveAd()
02-18 14:16:17.979: W/Ads(813): Not enough space to show ad! Wants: <320, 50>, Has: <288, 430>
答案 0 :(得分:30)
您的RelativeLayout的左右填充占用了AdView所需的空间。
将RelativeLayout配置更改为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:paddingLeft="0dp"
android:paddingRight="0dp"
...
>
您可能还想将AdView配置更改为:
<com.google.ads.AdView
android:id="@+id/adMobadView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
/>
因为您并不希望AdView使用整个屏幕。
答案 1 :(得分:0)
检查屏幕和衬垫的尺寸,看看它是否真的适合。
另外,尝试将Adview更改为:
<com.google.ads.AdView
android:id="@+id/adMobadView"
android:layout_width="320dp"
android:layout_height="50dp"
ads:adSize="BANNER"
ads:adUnitId="@string/admobid"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:loadAdOnCreate="true" />
答案 2 :(得分:0)
to show ads inside relative layout you can follow this design
<RelativeLayout
android:id="@+id/relativeLayoutpopular"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@+id/bottomBar"
android:layout_below="@+id/relativeLayoutadd"
android:layout_marginTop="5dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="3dp">
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_weight="0.9"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
android:background="#2d2f2f"
ads:adUnitId="admobId" />
</LinearLayout>
</RelativeLayout>
答案 3 :(得分:0)
我遇到了与NativeExpressAdView相同的问题。如果您在布局中添加填充或边距,我认为,修复广告的尺寸宽度不是一个好主意。我打算用编程方式做
第一步:获取屏幕的dp大小
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
//float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
dpWidth = displayMetrics.widthPixels / displayMetrics.density;
醇>
2.第二步:
mAdView.setAdSize(new AdSize((int) dpWidth, 150));
答案 4 :(得分:-1)
只需在res-&gt; values-&gt; dimens.xml
中将边距设置为0dp<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>