Android - AdView 缺少必需的 XML 属性“adSize”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20153270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:22:42  来源:igfitidea点击:

Android - AdView missing required XML attribute 'adSize'

androidadmob

提问by Deimantas Brandi?auskas

My head almost exploded today because the entire day I tried to solve the problem with my AdView from AdMob. I'm getting an error mentioned in the title. I googled through 3 pages but nothing. Same error whatever I do. This is my XML file:

今天我的头几乎要爆炸了,因为我一整天都在试图用 AdMob 的 AdView 解决这个问题。我收到标题中提到的错误。我用谷歌搜索了 3 页,但一无所获。无论我做什么都犯同样的错误。这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.48"
        android:src="@drawable/butters10" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="42dp" >

        <Button
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/prev" />

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/next" />
    </RelativeLayout>

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:loadAdOnCreate="true"
        app:adSize="BANNER"
        app:adUnitId="------------------------------" >
    </com.google.ads.AdView>

</LinearLayout>

I tried to change the appkeyword to adsbut the problem still shows up.

我尝试将app关键字更改为,ads但问题仍然出现。

Another thing is I actually want to add the same ad this the same unit-id to more than one activity. I added this exactly code to my first activity xml file, and everything works fine, but in the second activity it doesn't work at all. Thank you for any help.

另一件事是我实际上想将具有相同单元 ID 的相同广告添加到多个活动中。我将此代码添加到我的第一个活动 xml 文件中,一切正常,但在第二个活动中它根本不起作用。感谢您的任何帮助。

回答by Amir Alagic

To make it work with Google Play services use:

要使其与 Google Play 服务一起使用,请使用:

xmlns:ads="http://schemas.android.com/apk/res-auto"

xmlns:ads="http://schemas.android.com/apk/res-auto"

in your XML file.

在您的 XML 文件中。

回答by Ken Wolf

Change

改变

xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"

to

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

All the documentation seems to refer to the namespace as being at /lib/not /libs/

所有文档似乎都将命名空间称为 at /lib/not/libs/

And reference the tags using ads

并使用引用标签 ads

<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:loadAdOnCreate="true"
    ads:adSize="BANNER"
    ads:adUnitId="------------------------------" />

回答by Meanman

My requirement was to be able to dynamically pass in ad unit IDs instead of specifying them in xml. Turns out the framework does not like a mix of these, so I am creating my ads 100% programmatically:

我的要求是能够动态传入广告单元 ID,而不是在 xml 中指定它们。事实证明该框架不喜欢这些的混合,所以我 100% 以编程方式创建我的广告:

public class AdMobBanner extends FrameLayout {

private AdView adView;

public AdMobBanner(Context context, AdParams params) {
    super(context);

    adView = new AdView(context);
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER;
    adView.setLayoutParams(layoutParams);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(params.getAdUnitId());
    addView(adView);
}

public void showAdvert(AdParams params) {
    AdRequest.Builder builder = new AdRequest.Builder();

    for (String id: params.getTestDevices()) {
        builder.addTestDevice(id);
    }
    adView.loadAd(builder.build());
}

@Override
public void pause() {
    adView.pause();
}

@Override
public void resume() {
    adView.resume();
}

}

The AdParams class is my own class and you can replace it with your own class, or pass in strings, but the general solution is posted here.

AdParams 类是我自己的类,您可以将其替换为您自己的类,或传入字符串,但通用解决方案已发布在此处。

回答by sumitarora

The "loadAdOnCreate" and "testDevices" XML attributes no longer available. To make your ad working with Google Play Services

“loadAdOnCreate”和“testDevices”XML 属性不再可用。使您的广告与 Google Play 服务配合使用

Here is the XML layout

这是 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"/>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       ads:adSize="BANNER"
                       ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>

And here is your java file

这是你的java文件

package com.google.example.gms.ads.xml;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.app.Activity;
import android.os.Bundle;

/**
 * A simple {@link Activity} which embeds an AdView in its layout XML.
 */
public class BannerXMLSample extends Activity {

  private static final String TEST_DEVICE_ID = "INSERT_YOUR_TEST_DEVICE_ID_HERE";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice(TEST_DEVICE_ID)
        .build();
    adView.loadAd(adRequest);
  }
}

Refer the example here https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/banner-xml

请参阅此处的示例 https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/banner-xml

回答by metomero

Add android:theme="@android:style/Theme.Translucent"to your Android Manifest after meta-data

之后添加android:theme="@android:style/Theme.Translucent"到您的 Android 清单meta-data

Screenshot

Screenshot