【问题标题】:How to integrate Mopub Native ads in between content如何在内容之间集成 Mopub 原生广告
【发布时间】:2016-03-10 05:29:29
【问题描述】:

我已将 Mopub 原生广告集成到列表视图中,一切正常,但我想在我的内容之间而不是列表视图中显示原生广告。

我试过了

MoPubNative.MoPubNativeNetworkListener moPubNativeListener = new MoPubNative.MoPubNativeNetworkListener() {
        @Override
        public void onNativeLoad(NativeAd nativeAd) {
            // ...
        }

        @Override
        public void onNativeFail(NativeErrorCode errorCode) {
            // ...
        }
    };

    MoPubNative moPubNative = new MoPubNative(SingleActivity.this, "ffb8734de73e4d62b93bae99c06db41f", moPubNativeListener);

    ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)
            .mainImageId(R.id.native_ad_main_image)
            .iconImageId(R.id.native_ad_icon_image)
            .titleId(R.id.native_ad_title)
            .privacyInformationIconImageId(R.id.native_ad_daa_icon_image)
            .textId(R.id.native_ad_text)
            .build();

    MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(viewBinder);
    moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);

    Location exampleLocation = new Location("example_location");
    exampleLocation.setLatitude(23.1);
    exampleLocation.setLongitude(42.1);
    exampleLocation.setAccuracy(100);

    //Specify which native assets you want to use in your ad.
    EnumSet<RequestParameters.NativeAdAsset> assetsSet = EnumSet.of(RequestParameters.NativeAdAsset.TITLE,
            RequestParameters.NativeAdAsset.TEXT,
            RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
            RequestParameters.NativeAdAsset.ICON_IMAGE);

    RequestParameters requestParameters = new RequestParameters.Builder()
            .keywords("gender:m,age:27")
            .location(exampleLocation)
            .desiredAssets(assetsSet)
            .build();

    moPubNative.makeRequest(requestParameters);

但是你可以看到我正在加载布局

  ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)

但是如何在我的详细活动内容之间呈现此布局。

谢谢。

【问题讨论】:

    标签: android ads mopub


    【解决方案1】:

    Mopub原生手动广告整合示例

    首先定义你的视图 native_advetisement.xml

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
    
        <ImageView
            android:id="@+id/native_ad_main_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            />
    
        <ImageView
            android:id="@+id/native_ad_icon_image"
            android:layout_width="20dp"
            android:layout_height="20dp"
            />
    
        <ImageView
            android:id="@+id/native_ad_daa_icon_image"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_below="@+id/native_ad_main_image"/>
    
        <TextView
            android:id="@+id/native_ad_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/native_ad_daa_icon_image"
            android:textSize="12dp"
           android:layout_below="@+id/native_ad_main_image"/>
    
        <TextView
            android:id="@+id/native_ad_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/black"
            android:layout_toRightOf="@+id/native_ad_daa_icon_image"
            android:textSize="11dp"
            android:layout_marginLeft="20dp"
            android:layout_below="@+id/native_ad_title"
            android:layout_marginBottom="10dp"/>
    
    
    </RelativeLayout>
    
    </LinearLayout>
    

    然后准备你的组件。

    公共类 AdverNative 扩展 LinearLayout {

    private static String LOG_TAG = AdverNative.class.getName();
    private Context mContext;
    Activity activity;
    
    public AdverNative(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        this.activity = (Activity) context;
        initView();
    }
    
    
    private void initView() {
        final ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_advetisement)
                .titleId(R.id.native_ad_title)
                .textId(R.id.native_ad_text)
                .mainImageId(R.id.native_ad_main_image)
                .iconImageId(R.id.native_ad_icon_image)
                .privacyInformationIconImageId(R.id.native_ad_daa_icon_image)
                .build();
        MoPubNative.MoPubNativeNetworkListener moPubNativeNetworkListener = new MoPubNative.MoPubNativeNetworkListener() {
            @Override
            public void onNativeLoad(NativeAd nativeAd) {
    
    
                AdapterHelper ah = new AdapterHelper(mContext, 0, 3);
                View v = ah.getAdView(null, AdverNative.this, nativeAd, viewBinder);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    
                addView(v, params);
            }
    
            @Override
            public void onNativeFail(NativeErrorCode nativeErrorCode) {
    
            }
        };
    
        MoPubNative moPubNative = new MoPubNative(activity, Constants.CODE_NATIVE, moPubNativeNetworkListener);
        moPubNative.registerAdRenderer(new MoPubStaticNativeAdRenderer(viewBinder));
        EnumSet<RequestParameters.NativeAdAsset> assetsSet = EnumSet.of(
                RequestParameters.NativeAdAsset.TITLE,
                RequestParameters.NativeAdAsset.TEXT,
                RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
                RequestParameters.NativeAdAsset.MAIN_IMAGE,
                RequestParameters.NativeAdAsset.ICON_IMAGE,
                RequestParameters.NativeAdAsset.STAR_RATING);
    
        Location exampleLocation = new Location("example_location");
        exampleLocation.setLatitude(23.1);
        exampleLocation.setLongitude(42.1);
        exampleLocation.setAccuracy(100);
        final String keywords = "";
    
        RequestParameters mRequestParameters = new     RequestParameters.Builder()
                .location(exampleLocation)
                .keywords(keywords)
                .desiredAssets(assetsSet)
                .build();
    
        moPubNative.makeRequest(mRequestParameters);
      }
    
    }
    

    并像这样使用

    <AdverNative
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    </AdverNative>
    

    【讨论】:

      【解决方案2】:

      这是做到这一点的方法,至少在最新最好的 MoPub SDK 中是这样。

          public void onNativeLoad(NativeAd nativeAd) {
      
              RelativeLayout adParent = findViewById(R.id.ad_holder);
              View adView = nativeAd.createAdView(getActivity(), adParent);
              nativeAd.prepare(adView);
              nativeAd.renderAdView(adView);
              adParent.addView(adView);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多