缺少必需的 XML 属性“adsize”(谷歌播放服务)

Sid*_*mit 0 android admob google-play-services

有很多这样的问题,但它不是重复的

因为我已经使用过 xmlns:ads="http://schemas.android.com/apk/res-auto" 并且我主要用 Java 编写代码

我想使用谷歌播放服务来投放广告

我使用了下面的java代码

    com.google.android.gms.ads.AdView adView = (com.google.android.gms.ads.AdView) activity.findViewById(R.id.adview);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(activity.getString(R.string.admob_id));
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
Run Code Online (Sandbox Code Playgroud)

并遵循xml代码

 <com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     />
Run Code Online (Sandbox Code Playgroud)

但我在横幅中遇到错误就像

缺少必需的 XML 属性“adsize”

Bir*_*dia 5

将命名空间更改为

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
Run Code Online (Sandbox Code Playgroud)

广告文件

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/adview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

笔记 :

将此命名空间用于新 SDK

xmlns:ads="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)