首页 > 解决方案 > 更改原生 Admob 样式

问题描述

我曾经在我的应用程序中有一个原生广告,我使用以下代码来创建它并设置它的样式:

MobileAds.initialize( itemView.getContext(), AppConstants.APP_ADS_ID );
AdLoader adLoader = new AdLoader.Builder( itemView.getContext(), itemView.getContext().getResources().getString( R.string.Native_Ads ) )
        .forUnifiedNativeAd( unifiedNativeAd -> {
            NativeTemplateStyle styles = new
                    NativeTemplateStyle.Builder().withMainBackgroundColor( new ColorDrawable( itemView.getContext().getResources().getColor( R.color.colorWhite ) ) ).build();

            TemplateView template = itemView.findViewById( R.id.my_template );
            template.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 300 ) );
            template.setStyles( styles );
            template.setNativeAd( unifiedNativeAd );

        } )
        .build();

adLoader.loadAd( new AdRequest.Builder().build() );

但是,forUnifiedNativeAd已弃用,并且根据我需要使用的文档:

AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110")
    .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
        @Override
        public void onNativeAdLoaded(NativeAd NativeAd) {
            // Show the ad.
        }
    })
    .withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(LoadAdError adError) {
            // Handle the failure by logging, altering the UI, and so on.
        }
    })
    .withNativeAdOptions(new NativeAdOptions.Builder()
            // Methods in the NativeAdOptions.Builder class can be
            // used here to specify individual options settings.
            .build())
    .build();

关于如何使用新方法保持风格的任何想法?

谢谢

标签: javaandroidadmob

解决方案


推荐阅读