我想在androidstudio中的XML文件中显示原生广告视图,但它给了我一个错误
基本上,我的问题是,当我在XML文件中添加本机的ads_视图时,它会给我错误(“缺少必需的XML_属性“adSize”) XML文件:我想在androidstudio中的XML文件中显示原生广告视图,但它给了我一个错误,android,Android,基本上,我的问题是,当我在XML文件中添加本机的ads_视图时,它会给我错误(“缺少必需的XML_属性“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:
<?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>
我找到了一个解决方案,如果您这样使用它,它将起作用:
<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"/>
或者可以通过使缓存无效并重新启动android studio来解决,或者尝试将大小更改为ads:adSize=“300x250”肯定能用。试着在java端设置广告大小。我该怎么做?请你给我发一段代码作为例子。我知道这是一个奇怪的解决方案,但你能尝试在android studio中使缓存无效并重新启动吗?或者在java中设置它,你可以做最后的AdView AdView=new AdView(getActivity());adView.setAdUnitId(“YourId”);adView.setAdSize(AdSize.BANNER);(在这里发布问题时,请保持你的帖子清晰简洁。问候语、你有多困、你搜索了多少、你正在等待答案等都是无关紧要的,最好不要添加。它们只会让你的帖子变得杂乱无章,不易阅读)在应用我的建议@Shanimalik时,你是否也遇到了同样的错误谢谢你,先生,现在一切正常……我使用这一步骤使缓存失效,并重新启动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"/>
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());