OStack程序员社区-中国程序员成长平台

标题: java - Admob 广告在屏幕方向上未正确调整大小 [包括图片] [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-9 05:01
标题: java - Admob 广告在屏幕方向上未正确调整大小 [包括图片]

我在我的应用中使用 Admob 转换广告,但遇到了问题。每当我将屏幕切换到横向模式时,广告就会出现,但它的大小与纵向模式下的大小相同。在我将 list 中的这个 xml 声明添加到我的主要 Activity 中后发生了这个问题,这是保持应用程序主要部分顺利运行所必需的:

android:configChanges="orientation|keyboardHidden|screenSize"

我在我的广告中使用智能横幅的尺寸:

ads:adSize="SMART_BANNER"

我已经附上了这个问题的图片:

This is what it looks like in portrait mode. It runs perfectly This is what it looks like after I turn my phone sideways. The ad still shows but it doesn't resize to the fill width

我必须做些什么才能让广告在横向模式下正确调整大小而不删除

android:configChanges="orientation|keyboardHidden|screenSize"  

在我的主要 Activity 的 list 中?



Best Answer-推荐答案


我就是这样解决的:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        orientation_changed = true;
        renewAd();
    }

    private void renewAd() {
        AdView ad = (AdView) findViewById(R.id.adView); 
        LayoutParams lp = (LayoutParams) ad.getLayoutParams();
        // change relative layout for your layout which contains the adView
        RelativeLayout parent = (RelativeLayout) ad.getParent();
        parent.removeView(ad);      
        AdView newAd = new AdView(this, AdSize.SMART_BANNER, "YOUR ID");
        newAd.setId(R.id.adView);
        newAd.setLayoutParams(lp);      
        parent.addView(newAd);      
        newAd.loadAd(new AdRequest());
    }

问候

关于java - Admob 广告在屏幕方向上未正确调整大小 [包括图片],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929963/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4