Android Admob advert in PreferenceActivity

前端 未结 7 581
暖寄归人
暖寄归人 2020-12-04 22:32

Is there a way to add an admob advert to a PreferenceActivity? How to?

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 22:49

    There is some changes to P.Melch answer in Adpreference class is like below (because its not working with latest google play ads library):

        public class AdPreference extends Preference {
    
        public AdPreference(Context context, AttributeSet attrs, int defStyle) {super    (context, attrs, defStyle);}
        public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
        public AdPreference(Context context) {super(context);}
    
        @Override
        protected View onCreateView(ViewGroup parent) {
            // this will create the linear layout defined in ads_layout.xml
            View view = super.onCreateView(parent);
    
            // the context is a PreferenceActivity
            Activity activity = (Activity)getContext();
    
            AdView   adView = new AdView(getContext());
            adView.setAdUnitId("");
                    adView.setAdSize(AdSize.BANNER);
            AdRequest adRequest = new AdRequest.Builder().build();
            adView.loadAd(adRequest);
            ((LinearLayout)view).addView(adView);
            return view;
        }
    }
    

提交回复
热议问题