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