首页 > 解决方案 > 我想在 Android Studio 的 XML 文件中显示 Native_Ads_view 但它给了我错误

问题描述

基本上我的问题是,当我在 XML 文件中添加 native_ads_view 时,它给了我错误(“缺少必需的 XML_attribute “adSize”)。

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#72B2D1"
    tools:context=".SplashScreen">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.google.android.gms.ads.AdView>

    <com.google.android.gms.ads.NativeExpressAdView
        android:id="@+id/sndNativeAds"
        ads:adUnitId="ca-app-pub-3940256099942544/2247696110"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="300x200"
        app:layout_constraintBottom_toTopOf="@+id/btn_move"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/adView">
    </com.google.android.gms.ads.NativeExpressAdView>

    <Button
        android:id="@+id/btn_move"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="Move To Next"
        android:textSize="20sp"
        android:background="#000000"
        android:textColor="#fff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

错误

Required XML_attribute "adSize" was missing

标签: android

解决方案


我找到了一个解决方案,如果您像这样使用它,它将起作用:

<com.google.android.gms.ads.NativeExpressAdView
 android:id="@+id/adView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 ads:adSize="FULL_WIDTHx80"
 ads:adUnitId="@string/native_ad_unit"/>

NativeExpressAdView或者当我以编程方式添加并将其删除时,它已解决XML,如下所示。

nativeAd = new NativeExpressAdView(this);
int widthInDP = context.getResources().getConfiguration().screenWidthDp;
    widthInDP -= context.getResources().getDimension(R.dimen.activity_horizontal_margin);
nativeAd.setAdSize(new AdSize(widthInDP, 250)); 
nativeAd.setAdUnitId("myAdUnitId");

// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

// Add the NativeExpressAdView to the view hierarchy.
layout.addView(nativeAd);

// Start loading the ad.
nativeAd.loadAd(adRequestBuilder.build());

或者它可以通过使缓存无效并重新启动你的android工作室来解决,或者尝试将大小更改为 ads:adSize="300x250"肯定会工作。


推荐阅读