java - 偏好 Activity 中的广告 "Not enough space to show ad! Wants: <480, 75>, Has: <432, 1073741823>"

标签 java android admob

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

最佳答案

我假设您尝试按照以下示例进行操作: Android Admob advert in PreferenceActivity

我认为错误的是: 您扩展了一个“首选项”,但您正试图从中定义一个线性布局。相反,只需将其直接添加到您的首选项屏幕即可。

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

...

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

...
</PreferenceScreen>

关于java - 偏好 Activity 中的广告 "Not enough space to show ad! Wants: <480, 75>, Has: <432, 1073741823>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11488454/

相关文章:

android - notifyDataSetChanged 在 android 中不起作用

java - Android文件存储存储内存位置

java - admob 不工作是因为(我认为)google play 服务

java - 在 Android 中从 firebase 获取日历

java程序获取ip isp信息

java - 如何从 opencv for java 中的位创建垫子?

java - 用于将数据从 Excel 插入 MySQL 的 JDBC 库

google-play - 在 ac.loadClass 中得到一个奇怪的 ClassNotFoundException

java - 如何在 Xamarin.Android 中实现 RewardedAdLoadCallback?

java - DexGuard - 使用 Robolectric 加密类 NullPointerException