如何在我的应用程序中显示 admob 原生广告?
How do I show an admob native ad in my App?
我有一个带有导航组件的 activity 应用程序。在我的应用程序上,我正在尝试实施 AdMob 原生广告(使用来自 google 开发人员文档的测试 unitId),但无论我尝试了多少,它都没有显示 [=14] =] 但我不明白我将如何用那里的代码显示它。我也尝试寻找示例和教程,但其中大部分都是我不懂的语言。任何帮助将不胜感激!
我的清单
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/appId"/> // it is a sample
my_nativead.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="3dp"
android:background="@drawable/linear_border_style">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad"
android:textColor="@color/white"
android:background="@color/blue"
android:backgroundTint="@color/blue"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/nativeAdSmallImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_email"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nativeAdSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="Ad Title"/>
<com.google.android.gms.ads.nativead.MediaView
android:id="@+id/nativeAdSmallMedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/nativeAdSmallName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:text="Ad description"/>
<TextView
android:id="@+id/nativeAdSmallDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:text="Ad description"/>
<Button
android:id="@+id/nativeAdSmallButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="visit site"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
我的主要活动
frame = findViewById(R.id.myAd)
MobileAds.initialize(this)
val adBuilder = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") // the unitId is a sample
.forNativeAd { nativeAd ->
desplayNativeAd(frame, nativeAd)
}
adBuilder.build()
}
fun desplayNativeAd(parent: ViewGroup, ad: NativeAd){
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val adView = inflater.inflate(R.layout.native_small_adview, parent) as NativeAdView
val headlineView = adView.findViewById<TextView>(R.id.nativeAdSmallTitle)
headlineView.text = ad.headline
adView.headlineView = headlineView
val advertiser = adView.findViewById<TextView>(R.id.nativeAdSmallName)
advertiser.text = ad.advertiser
adView.advertiserView = advertiser
val imageAdView = adView.findViewById<ImageView>(R.id.nativeAdSmallImage)
Glide.with(this)
.load(ad.icon)
.into(imageAdView)
val mediaView = adView.findViewById<MediaView>(R.id.nativeAdSmallMedia)
adView.mediaView = mediaView
adView.setNativeAd(ad)
parent.removeAllViews()
parent.addView(adView)
}
我的 mainActivity.xml // 我有很多关于这个的 xml 代码,但我只展示了这个,这是一个 framelyout处理原生广告
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/myAd"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
我刚刚解决了这个问题,我只从我的主片段而不是我的主片段调用框架布局 activity 并且我在片段的 onViewCreated 方法中使用它的函数编写了 adBuilder。
这是我写的..
// I wrote it in onViewCreated meathod
MobileAds.initialize(requireContext())
//native ad
val adBuilder = AdLoader.Builder(requireContext(), "your_ad_id")
.forNativeAd { nativeAd ->
val adView = layoutInflater.inflate(R.layout.native_big_adiew, null) as NativeAdView
displayBigNativeAd(adView, nativeAd)
frame.removeAllViews()
frame.addView(adView)
}
.build()
adBuilder.loadAd(AdRequest.Builder().build())
// outside onViewCreated method
private fun displayBigNativeAd(adView: NativeAdView, ad: NativeAd) {
val headline = adView.findViewById<TextView>(R.id.nativeAdBigTitle)
headline.text = ad.headline
adView.headlineView = headline
val advertiser = adView.findViewById<TextView>(R.id.nativeAdBigName)
advertiser.text = ad.advertiser
adView.advertiserView = adView
val icon = adView.findViewById<ImageView>(R.id.nativeAdBigImage)
icon.setImageDrawable(ad.icon.drawable)
adView.iconView = icon
val mediaView = adView.findViewById<MediaView>(R.id.nativeAdBigMediaView)
adView.mediaView = mediaView
val button = adView.findViewById<Button>(R.id.nativeAdBigButton)
adView.callToActionView = button
button.setText(ad.store)
val body = adView.findViewById<TextView>(R.id.nativeAdBigDesc)
body.text = ad.body
adView.bodyView = body
val price = adView.findViewById<TextView>(R.id.nativeAdBigPrice)
price.text = ad.price
adView.priceView = price
adView.setNativeAd(ad)
}
我有一个带有导航组件的 activity 应用程序。在我的应用程序上,我正在尝试实施 AdMob 原生广告(使用来自 google 开发人员文档的测试 unitId),但无论我尝试了多少,它都没有显示 [=14] =] 但我不明白我将如何用那里的代码显示它。我也尝试寻找示例和教程,但其中大部分都是我不懂的语言。任何帮助将不胜感激!
我的清单
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/appId"/> // it is a sample
my_nativead.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="3dp"
android:background="@drawable/linear_border_style">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad"
android:textColor="@color/white"
android:background="@color/blue"
android:backgroundTint="@color/blue"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/nativeAdSmallImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_email"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nativeAdSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="Ad Title"/>
<com.google.android.gms.ads.nativead.MediaView
android:id="@+id/nativeAdSmallMedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/nativeAdSmallName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:text="Ad description"/>
<TextView
android:id="@+id/nativeAdSmallDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:text="Ad description"/>
<Button
android:id="@+id/nativeAdSmallButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="visit site"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
我的主要活动
frame = findViewById(R.id.myAd)
MobileAds.initialize(this)
val adBuilder = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") // the unitId is a sample
.forNativeAd { nativeAd ->
desplayNativeAd(frame, nativeAd)
}
adBuilder.build()
}
fun desplayNativeAd(parent: ViewGroup, ad: NativeAd){
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val adView = inflater.inflate(R.layout.native_small_adview, parent) as NativeAdView
val headlineView = adView.findViewById<TextView>(R.id.nativeAdSmallTitle)
headlineView.text = ad.headline
adView.headlineView = headlineView
val advertiser = adView.findViewById<TextView>(R.id.nativeAdSmallName)
advertiser.text = ad.advertiser
adView.advertiserView = advertiser
val imageAdView = adView.findViewById<ImageView>(R.id.nativeAdSmallImage)
Glide.with(this)
.load(ad.icon)
.into(imageAdView)
val mediaView = adView.findViewById<MediaView>(R.id.nativeAdSmallMedia)
adView.mediaView = mediaView
adView.setNativeAd(ad)
parent.removeAllViews()
parent.addView(adView)
}
我的 mainActivity.xml // 我有很多关于这个的 xml 代码,但我只展示了这个,这是一个 framelyout处理原生广告
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/myAd"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
我刚刚解决了这个问题,我只从我的主片段而不是我的主片段调用框架布局 activity 并且我在片段的 onViewCreated 方法中使用它的函数编写了 adBuilder。
这是我写的..
// I wrote it in onViewCreated meathod
MobileAds.initialize(requireContext())
//native ad
val adBuilder = AdLoader.Builder(requireContext(), "your_ad_id")
.forNativeAd { nativeAd ->
val adView = layoutInflater.inflate(R.layout.native_big_adiew, null) as NativeAdView
displayBigNativeAd(adView, nativeAd)
frame.removeAllViews()
frame.addView(adView)
}
.build()
adBuilder.loadAd(AdRequest.Builder().build())
// outside onViewCreated method
private fun displayBigNativeAd(adView: NativeAdView, ad: NativeAd) {
val headline = adView.findViewById<TextView>(R.id.nativeAdBigTitle)
headline.text = ad.headline
adView.headlineView = headline
val advertiser = adView.findViewById<TextView>(R.id.nativeAdBigName)
advertiser.text = ad.advertiser
adView.advertiserView = adView
val icon = adView.findViewById<ImageView>(R.id.nativeAdBigImage)
icon.setImageDrawable(ad.icon.drawable)
adView.iconView = icon
val mediaView = adView.findViewById<MediaView>(R.id.nativeAdBigMediaView)
adView.mediaView = mediaView
val button = adView.findViewById<Button>(R.id.nativeAdBigButton)
adView.callToActionView = button
button.setText(ad.store)
val body = adView.findViewById<TextView>(R.id.nativeAdBigDesc)
body.text = ad.body
adView.bodyView = body
val price = adView.findViewById<TextView>(R.id.nativeAdBigPrice)
price.text = ad.price
adView.priceView = price
adView.setNativeAd(ad)
}