4

我正在尝试在偏好活动中展示广告,但它从未出现。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。这确实会显示广告,但它会被推到屏幕的右侧,并且占用的尺寸略小于预期尺寸的一半(并切断了一半的广告)。

4

3 回答 3

0

看到类似的问题

将父视图组的填充设置为 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;    
}
于 2013-10-28T19:54:16.450 回答
0

我假设您尝试遵循此示例: PreferenceActivity 中的 Android Admob 广告

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

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

...

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

...
</PreferenceScreen>
于 2012-07-15T01:06:34.940 回答
0

这可以通过使用AdSize.FLUIDAdSize.SMART_BANNER动态调整 AD 的宽度和高度来轻松解决。了解更多信息

于 2017-05-26T07:23:18.857 回答