在自定义视图中显示广告横幅

时间:2017-08-06 20:12:41

标签: android ads appodeal

我有自定义视图活动,我想在其中显示广告。只是屏幕底部的横幅视图。我正在使用Appodeal的广告API。 这是我到目前为止所尝试的:

public class main extends AppCompatActivity  {

    BannerView bannerView;
    View customView;

    public static void loadAd(Context context, Activity activity) {
            Appodeal.setTesting(true);
            Appodeal.disableNetwork(context, "cheetah");
            Appodeal.disableLocationPermissionCheck();
            Appodeal.setBannerViewId(R.id.appodealBannerView);
            Appodeal.initialize(activity, context.getString(R.string.app_key), Appodeal.BANNER_VIEW);
            Appodeal.show(activity, Appodeal.BANNER_VIEW);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customView = new CustomView(this, null);
        setContentView(customView);

        bannerView = new BannerView(this, null);
        bannerView.setId(R.id.appodealBannerView);
    }

    @Override
    protected void onResume() {
        super.onResume();

        loadAd(this, this);
    }

    class CustomView extends View {
        CustomView(Context ctx, AttributeSet attrs) {
            super(ctx, attrs);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            bannerView.draw(canvas);

            invalidate();
        }
    }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

你可以使用如下的xml布局:

<RelativeLayout
 ...>
 <PathToCustomView.CustomView
  ...
  />

 <PathToBannerView.BannerView
  android:below="+id/myCustomviewId"
  ...
  />

</RelativeLayout>