簡體   English   中英

Admob廣告未在屏幕方向上正確調整大小[包含圖片]

[英]Admob ad not resizing correctly upon screen orientation [Includes Pictures]

我在我的應用中使用Admob廣告,但我遇到了問題。 每當我將屏幕轉為橫向模式時,廣告就會顯示,但它與縱向模式下的尺寸相同。 在我的清單中將此xml聲明添加到我的主要活動之后發生此問題,這是保持應用程序主要部分順利運行所必需的:

android:configChanges="orientation|keyboardHidden|screenSize"

我在廣告中使用智能橫幅尺寸:

ads:adSize="SMART_BANNER"

我附上了這個問題的圖片:

這是肖像模式下的樣子。它完美運行這是我把手機側身后的樣子。廣告仍會顯示,但不會調整到填充寬度

如何在橫向模式下正確調整廣告大小而不刪除,我該怎么辦?

android:configChanges="orientation|keyboardHidden|screenSize"  

在我的主要活動的清單?

這是我解決它的方式:

@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());
    }

問候

與其他響應一致(但將其更新為當前的AdMob SDK -v7.5-並提供完整代碼),活動的onConfigurationChanged()方法需要包括銷毀和創建廣告視圖:

// Remove the ad keeping the attributes
AdView ad = (AdView) myactivity.findViewById(R.id.adView);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ad.getLayoutParams();
LinearLayout parentLayout = (LinearLayout) ad.getParent();
parentLayout.removeView(ad);

// Re-initialise the ad
ad.destroy();
ad = new AdView(parent);
ad.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
ad.setAdUnitId(myactivity.getString(R.string.banner_ad_unit_id));
ad.setId(R.id.adView);
ad.setLayoutParams(lp);
parentLayout.addView(ad);

// Re-fetch add and check successful load
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice(parent.getString(R.string.test_device_id))
    .build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM