在所有活动中展示相同的AdMob横幅广告

时间:2018-01-27 07:48:16

标签: android android-layout android-activity admob

现在有三项活动,当我转换到第二项活动时,新的横幅广告正在加载

所以我制作了额外的课程,并使用横幅的独立布局将其加载到所有活动的底部

在所有活动和setupAdAtBottom()中扩展的额外横幅类;只是被称为展示横幅广告

现在问题是,布局正在加载,但横幅广告没有显示

横幅布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner" >
    </com.google.android.gms.ads.AdView>

</LinearLayout>

横幅加载类

package mohammedi.computers.kids.encyclopedia;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class AdMobBanner extends AppCompatActivity {

    private FrameLayout content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void setSpaceForAd(int height) {

        // content.getChildAt(0).setPadding(0, 0, 0, 50);
        View child0 = content.getChildAt(0);
        FrameLayout.LayoutParams layoutparams = (android.widget.FrameLayout.LayoutParams) child0
                .getLayoutParams();
        layoutparams.bottomMargin = height;
        child0.setLayoutParams(layoutparams);

    }

    @SuppressLint("NewApi")
    protected void setupAdAtBottom() {
        content = findViewById(android.R.id.content);
        // inflate ad layout and set it to bottom by layouparams
        final LinearLayout ad = (LinearLayout) getLayoutInflater()
                .inflate(R.layout.ad_layout, null);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM;
        ad.setLayoutParams(params);

        // adding viewtreeobserver to get height of ad layout , so that
        // android.R.id.content will set margin of that height
        ViewTreeObserver vto = ad.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ad.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                int width = ad.getMeasuredWidth();
                int height = ad.getMeasuredHeight();
                Log.i("ad hight", height + "");
                setSpaceForAd(height);

            }

        });
        addLayoutToContent(ad);



    }

    private void addLayoutToContent(View ad) {
        // content.addView(ad);
        content.addView(ad);
        AdView mAdView = ad.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }

}

主要活动

public class MainActivity extends AdMobBanner {
    String[] dir = new String[]{"birds","mammals"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupAdAtBottom();

0 个答案:

没有答案