AdView 首次加载时未显示

Posted

技术标签:

【中文标题】AdView 首次加载时未显示【英文标题】:AdView Not Showing on first load 【发布时间】:2018-09-02 17:21:08 【问题描述】:

我一直在学习如何制作游戏,我想了解如何添加广告。广告显示?但只有在我关闭并重新打开后,“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();
        
    

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

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

【问题讨论】:

【参考方案1】:

部分答案我想出了如何让广告与底部对齐并居中。

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);
            
        );

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于AdView 首次加载时未显示的主要内容,如果未能解决你的问题,请参考以下文章

HTML5 - 在 Android WebView 中首次加载时未呈现画布

首次运行时未显示 Viewcontroller

Visual Studio 2017在首次运行时未显示编译异常

初始化地图时未出现谷歌地图标记

PF QueryCollection ViewController 中首次加载时未出现图像

Angular Material Stepper 在首次加载时未定义