AdView 在第一次加载时不显示

AdView Not Showing on first load

我一直在努力学习如何制作游戏,我想了解如何添加广告。广告显示?但是只有在我关闭并重新打开后,"SMART_BANNER" 也不起作用。我做错了什么?

public class MainActivity extends FragmentActivity {

    public GoogleApiClient apiClient;
    private MainActivity main = this;
    public GameSurface gameSurface;
    RelativeLayout layout;
    RelativeLayout adlayout;
    private Saver saver;

    private static final String HIGHSCORE = "highscore";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        apiClient = new GoogleApiClient.Builder(this)
                .addApi(Games.API)
                .addScope(Games.SCOPE_GAMES)
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(getBaseContext(), "Connection To Google Games Failed, No App Found Or No Internet", Toast.LENGTH_SHORT).show();
                    }
                }).build();

        MobileAds.initialize(this, getString(R.string.adappid));

        apiClient.connect();
        saver = Saver.getInstance(this);

        playerscores();

        // fullscreen
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        layout = new RelativeLayout(this);
        layout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        adView.loadAd(adRequestBuilder.build());

        //ads
        gameSurface = new GameSurface(this, main);
        layout.addView(gameSurface);

        layout.addView(adView);

        setContentView(layout);

    }
            // Set No Title
            //this.setContentView(new GameSurface(this));

    public void playerscores() {
        if (apiClient != null && apiClient.isConnected()) {
            Games.Leaderboards.loadCurrentPlayerLeaderboardScore(apiClient, "CgkI08DA0-sZEAIQAQ", LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
                    new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {

                        @Override
                        public void onResult(Leaderboards.LoadPlayerScoreResult arg0) {
                            LeaderboardScore c = arg0.getScore();
                            String score = c.getDisplayScore();
                            saver.saveString(HIGHSCORE, score);
                        }
                    });
        }
    }

    public void gameover() {
        if (apiClient != null && apiClient.isConnected()) {
            Games.Leaderboards.submitScore(apiClient, getString(R.string.leaderboard_highscores), GameSurface.HighScore);
        }
    }

    public void showLeaderboard() {
        if (apiClient != null && apiClient.isConnected()) {
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(apiClient, "CgkI08DA0-sZEAIQAQ"), 1);
        } else {
            apiClient.connect();
        }
    }
}

基本上我希望广告在应用程序打开后立即显示。 id 也喜欢固定在底部,我找不到办法做到这一点,我试过添加重力但adview没有这个属性。

任何关于我做错了什么的建议将不胜感激。

答案部分我弄清楚了如何让广告对齐底部并居中。

RelativeLayout.LayoutParams viewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);viewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        adView.setLayoutParams(viewParams);

编辑[已解决]

添加了一个 adview 监听器,阅读 admob 文档并看到各种监听器给了我一个想法! 然后让它重新绘制自己。

 adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adView.setVisibility(View.GONE);
                adView.setVisibility(View.VISIBLE);
            }
        });

您应该在加载广告之前添加视图,如果您想更快地展示广告,请尽快调用 setContentView() 然后加载广告