Admob本机预先实施

时间:2018-12-14 07:19:51

标签: admob native

您好,我正在尝试从ref https://developers.google.com/admob/android/native/advanced实施admob本机高级广告,但它无法正常工作,无法理解正确的方法,请引导我,或者如果有正确的示例,请与我分享,我的代码如下:

native_advance.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.formats.UnifiedNativeAdView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="270dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:text="Ad"
        android:textSize="16sp" />

    <com.google.android.gms.ads.formats.MediaView
        android:id="@+id/appinstall_media"
        android:layout_width="match_parent"
        android:layout_height="170dp" />

    <TextView
        android:id="@+id/appinstall_headline"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:textSize="16sp" />

    <Button
        android:id="@+id/appinstall_call_to_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

</com.google.android.gms.ads.formats.UnifiedNativeAdView>

主要活动中的框架:

<FrameLayout
android:id="@+id/native_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>

和主要活动

private void loadNativeAds() {
    if (getApplicationContext() != null) {


        AdLoader.Builder builder = new AdLoader.Builder(this, "my_admob_id")
                .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // Assumes you have a placeholder FrameLayout in your View layout
                        // (with id fl_adplaceholder) where the ad is to be placed.
                        FrameLayout frameLayout =
                                findViewById(R.id.native_frame);
                        // Assumes that your ad layout is in a file call ad_unified.xml
                        // in the res/layout folder
                        UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                                .inflate(R.layout.native_advance, null);
                        // This method sets the text, images and the native ad, etc into the ad
                        // view.
                        pupulateNativeAd(unifiedNativeAd, adView);
                        frameLayout.removeAllViews();
                        frameLayout.addView(adView);
                    }
                });
    }

}

1 个答案:

答案 0 :(得分:0)

这还不是全部。 您必须定义pupulateNativeAd(UnifiedNativeAd unifiedNativeAd,UnifiedNativeAdView adView)函数。

这是一个非常基本的定义:

private void pupulateNativeAd(UnifiedNativeAd unifiedNativeAd, UnifiedNativeAdView adView) {
        TextView adTitle = adView.findViewById(R.id.id_native_ad_title);
        TextView adBody = adView.findViewById(R.id.id_native_ad_title);

        adTitle.setText(unifiedNativeAd.getHeadline());
        adBody.setText(unifiedNativeAd.getBody());
    }

也可以参考此link以获得更大的图片。