2014-01-05 33 views
4

我在我的應用中使用Admob廣告,並遇到問題。每當我將屏幕轉爲橫向模式時,廣告都會顯示,但尺寸與縱向模式下相同。這個問題發生後,我加入我的清單這個XML聲明我的主要活動,這是必要的,以保持應用程序的主要部分平穩運行:Admob廣告未在屏幕方向上正確調整大小[包括圖片]

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" 
在清單

我的主要活動?

+0

第一張照片就是我的廣告看起來像畫像,這是很好,但第二圖爲什麼看起來像橫向模式,這是不好的,因爲它沒有正確調整大小。 – user3131263

+0

http://stackoverflow.com/questions/11281562/android-admob-resize-on-landscape –

回答

7

這是我如何解決它:

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

問候

+3

的潛在副本其實,AdView的構造函數在當前SDK中不可用 –

6

與其它反應線(但他們更新到當前的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 
mAdView.destroy(); 
mAdView = new AdView(parent); 
mAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER); 
mAdView.setAdUnitId(myactivity.getString(R.string.banner_ad_unit_id)); 
mAdView.setId(R.id.adView); 
mAdView.setLayoutParams(lp); 
parentLayout.addView(mAdView); 

// 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(); 
+0

不適用於橫向,視圖高度會更小 – vuhung3990

+1

這是正確的行爲。景觀中的高度很稀少,所以廣告變得更短,以便爲用戶提供更多的屏幕實時狀態。 –

相關問題