2014-09-19 76 views
1

我已閱讀了很多關於此主題的內容,但沒有任何結果。我已從Android開發者處獲得以下代碼:以編程方式在底部顯示admob廣告

private AdView adView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    addListenerOnButton(); 

    // Crear adView. 
    adView = new AdView(this); 
    adView.setAdUnitId("MY_UNIT_ID"); 
    adView.setAdSize(AdSize.BANNER); 

    // Buscar RelativeLayout suponiendo que se le ha asignado 
    // el atributo android:id="@+id/mainLayout". 
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.activity_main); 

    // Añadirle adView. 
    layout.addView(adView); 

    // Iniciar una solicitud genérica. 
    AdRequest adRequest = new AdRequest.Builder().build(); 

    // Cargar adView con la solicitud de anuncio. 
    adView.loadAd(adRequest); 
    } 

@Override 
    public void onPause() { 
    adView.pause(); 
    super.onPause(); 
    } 

    @Override 
    public void onResume() { 
    super.onResume(); 
    adView.resume(); 
    } 

    @Override 
    public void onDestroy() { 
    adView.destroy(); 
    super.onDestroy(); 
    } 

它顯示廣告,但它不在屏幕的底部。我也嘗試過使用LayoutParams,但任何工作。有任何想法嗎?謝謝。

回答

0

您可以手動設置座標,廣告驗證碼:

@Override 
public void onWindowFocusChanged (boolean hasFocus) { 
    //get display height 
    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int displayHeight = displaymetrics.heightPixels; 

    //get adView height 
    int adViewHeight = adView.getHeight(); 
    int viewStartX = displayHeight = adViewHeight; 
    adView.setX(viewStartX);  
} 

你應該得到的邏輯,如果代碼是不是100%我和我的電話寫