簡體   English   中英

廣告偏好活動“沒有足夠的空間來展示廣告! 想要:<480,75>,有:<432,1073741823>“

[英]Ad in preference activity “Not enough space to show ad! Wants: <480, 75>, Has: <432, 1073741823>”

我正在嘗試在偏好活動中展示廣告,但它從未出現過。 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。 這確實顯示了廣告,但它被推到了屏幕的右側,占據了一半不到一半的廣告尺寸(並切斷了廣告的一半)。

我假設您嘗試按照此示例: PreferenceActivity中的Android Admob廣告

我認為錯誤的是:你擴展了一個'偏好',但是你試圖從中定義一個線性布局。 相反,只需將其直接添加到您的首選項屏幕即可。

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

...

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

...
</PreferenceScreen>

看到類似的問題

將父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;    
}

這可以通過使用AdSize.FLUIDAdSize.SMART_BANNER輕松解決,因為它可以動態調整AD的寬度和高度。 了解更多信息AdSize

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM