简体   繁体   English

如何以编程方式展示横幅广告

[英]how to show banner ads programmatically

I am trying to implement banner ad but it is messing up with my main layout which is a DrawView. 我正在尝试实施横幅广告,但它与我的主要布局DrawView混淆了。 Either mAdview is displayed or DrawView, but not together. 显示mAdview或DrawView,但不一起显示。 I want to show them together. 我想一起展示。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    size = getIntent().getIntExtra("SIZE", 3);
    setContentView(R.layout.activity_tile);
    drawView = new DrawView(this, size);
    setContentView(drawView);
    setTileSize();

    MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");
    mAdView = findViewById(R.id.adView);
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

}

XML created XML创建

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<view class="com.kilnake.patta.picpuzz.TileActivity$DrawView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"/>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />


</LinearLayout>

One problem you face here is how you use setContentView() . 您在这里面临的一个问题是如何使用setContentView() This function define which layout to display for this activity. 此功能定义要为此活动显示的布局。 What you do is : 您要做的是:

  1. setContentView(R.layout.activity_tile); : you tell to display the layout define in the file activity_tile.xml (what you call your main layout). :您告诉您要在文件activity_tile.xml中显示布局定义(您称其为主布局)。
  2. setContentView(drawView); : Tell to change what to display, stop display your main layout and display the DrawView :告诉更改要显示的内容,停止显示主布局并显示DrawView
  3. setContentView(layouut); : Cahnge again to stop display the DrawView and display layouut (which contains only the adView) :再次Cahnge停止显示DrawView并显示layouut (仅包含adView)

You need to call this function only once. 您只需要调用一次此函数。 Define your whole layout in the xml file, by adding the DrawView and the Adview in this file. 通过在该文件中添加DrawView和Adview,在xml文件中定义整个布局。 Then use only setContentView(R.layout.activity_tile); 然后仅使用setContentView(R.layout.activity_tile);

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

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