广告偏好活动“没有足够的空间展示广告!想要:< 480,75>,有:< 432,1073741823>”

时间:2012-07-15 00:27:16

标签: java android admob

我正在尝试在偏好活动中展示广告,但它从未出现过。 Logcat始终显示消息“没有足够的空间显示广告!想要:< 480,75>,有:< 432,1073741823>”

这就是我制作广告的方式。我有广告的自定义偏好

public class AdmobPreference extends Preference {

public AdmobPreference(Context context, AttributeSet attrs, int defStyle) {super    (context, attrs, defStyle);}
public AdmobPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdmobPreference(Context context) {super(context);}

@Override
protected View onCreateView(ViewGroup parent) {
    // this will create the linear layout defined in ads_layout.xml
    View view = super.onCreateView(parent);

    // the context is a PreferenceActivity
    Activity activity = (Activity)getContext();

    // Create the adView
    AdView adView = new AdView(activity, AdSize.BANNER, "MY KEY");

    ((LinearLayout)view).addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest request = new AdRequest();
    adView.loadAd(request);     

    return view;    
}
}

然后我将广告放入

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
   android:orientation="vertical"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">

</LinearLayout>

使用以下行将其放入首选项xml中:

<com.my.package.name android:layout="@layout/admob_preference" />

我可以更改它,因此布局设置为width = 480dip height = 75dip而不是wrap_content。这确实显示了广告,但它被推到了屏幕的右侧,占据了不到一半的广告尺寸(并减少了广告的一半)。

3 个答案:

答案 0 :(得分:0)

我假设你试图效仿这个例子: Android Admob advert in PreferenceActivity

我认为错误的是: 您扩展了“首选项”,但之后您尝试从中定义线性布局。相反,只需将其直接添加到您的首选项屏幕即可。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

...

<com.example.AdmobPreference android:layout="@layout/admob_preference"/>

...
</PreferenceScreen>

答案 1 :(得分:0)

请参阅similar question

将父viewGroup的填充设置为0为我解决了这个问题。

@Override
protected View onCreateView(ViewGroup parent) {
    //Next line solves the error by adjusting the padding of the parent ViewGroup
    parent.setPadding(0, 0, 0, 0);

    // this will create the linear layout defined in ads_layout.xml
    View view = super.onCreateView(parent);

    // the context is a PreferenceActivity
    Activity activity = (Activity)getContext();

    // Create the adView
    AdView adView = new AdView(activity, AdSize.BANNER, "MY KEY");

    ((LinearLayout)view).addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest request = new AdRequest();
    adView.loadAd(request);     

    return view;    
}

答案 2 :(得分:0)

使用AdSize.FLUIDAdSize.SMART_BANNER可以轻松解决这个问题,因为它会动态调整AD的宽度和高度。 了解更多信息 AdSize