android - 我想在 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
解决方案
我找到了一个解决方案,如果您像这样使用它,它将起作用:
<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"
肯定会工作。
推荐阅读
- python - Python:如何使用 .format() 将不同长度的列表解压缩为字符串
- c# - 如果检查两者都有值并且两者相等,则简化
- html - 如何使用formio在表单管理器的一种布局中设置提交表单
- java - SVG 无法找到我在自动化脚本中验证所需的图形元素的 xpath
- azure-sql-database - 何时在 Azure SQL DB 上禁用 TDE
- excel - 更改 Excel 中数字的格式
- scikit-learn - sklearn 中的精度、召回率和混淆矩阵问题
- jquery - 使用 jquery 和 ajax 动态更改文本颜色,具有不同的颜色、不同的文本和具有相同的属性/类
- java - 如何将 SQL(嵌套子查询)转换为 QueryBuilder 创建的查询
- python - 如何序列化 DRF 中的泛型关系?