2012-07-15 50 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