简体   繁体   English

如何在内容之间整合Mopub Native广告

[英]How to integrate Mopub Native ads in between content

I have integrated Mopub native ads in listview all working good but i want to show native ads in between my content not in listview. 我已经在列表视图中集成了Mopub原生广告,但一切正常,但是我希望在不在列表视图中的内容之间显示原生广告。

I tried this 我试过了

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);

but as you can see i am loading layout 但如您所见,我正在加载布局

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

but how to render this layout in between my content in detail activity. 但是如何在详细活动中在我的内容之间呈现此布局。

Thanks. 谢谢。

Mopub native manual advertisement integration example Mopub本机手动广告集成示例

First of all define your view native_advetisement.xml 首先定义您的视图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>

And then prepare your component. 然后准备您的组件。

public class AdverNative extends 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);
  }

}

And use like this 像这样使用

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

</AdverNative>

This is the way to do it, at least in latest and greatest MoPub SDK. 至少在最新,最出色的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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM