2012-07-15 68 views
4

我試圖在首選項活動中顯示廣告,但從未出現。 Logcat總是顯示消息「沒有足夠的空間顯示廣告!想要:< 480,75>,具有:< 432,1073741823>」廣告優先活動」沒有足夠的空間來顯示廣告!想要:<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。這確實會顯示廣告,但它會被推到屏幕的右側,佔用的尺寸略小於一半(並切斷了廣告的一半)。

回答

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> 
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;  
} 
0

這可以很容易通過使用AdSize.FLUIDAdSize.SMART_BANNER因爲它動態調整AD的寬度和高度來解決。 瞭解更多信息 AdSize

相關問題