2013-09-28 64 views
2

logcat只會給出一個錯誤,沒有足夠的空間顯示廣告。除了啓動屏幕上的admob廣告外,應用中的所有內容都可以正常工作。下面是XML沒有足夠的空間與admob顯示廣告

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/close" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10.0dip" 
    android:layout_marginRight="10.0dip" 
    android:text="Exit" 
    android:textSize="20.0dip" /> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <com.google.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adSize="SMART_BANNER" 
     ads:adUnitId="XXXXXX" 
     ads:loadAdOnCreate="true" 
     ads:testDevices="TEST_EMULATOR" > 
    </com.google.ads.AdView> 
</RelativeLayout> 

</LinearLayout> 

所有我想在屏幕上是一整頁的廣告和一個按鈕按下時,將關閉添加和推出的主要活動。該按鈕工作正常,但當該XML加載它不顯示廣告,並打開的窗口比屏幕小得多。

這是上面的佈局設置的.Java活動。

package com.androidsleepmachine.gamble; 

import com.google.ads.AdRequest; 
import com.google.ads.AdView; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Splash extends Activity { 

Button close; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    close = (Button) findViewById(R.id.close); 
    AdView adView = (AdView)this.findViewById(R.id.adView); 
    adView.loadAd(new AdRequest()); 
    close.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent("android.intent.action.Home"); 
      startActivity(i); 


     } 
    }); 
} 

} 
+1

你爲什麼把'AdView'在'RelativeLayout'? –

回答

1

您已將layout_height設置爲父級的wrap_content。用這個代替:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/close" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10.0dip" 
    android:layout_marginRight="10.0dip" 
    android:text="Exit" 
    android:textSize="20.0dip" /> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <com.google.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adSize="SMART_BANNER" 
     ads:adUnitId="XXXXXX" 
     ads:loadAdOnCreate="true" 
     ads:testDevices="TEST_EMULATOR" > 
    </com.google.ads.AdView> 
</RelativeLayout> 

</LinearLayout> 

編輯: 您需要使用的代碼在您的活動加載您的應用程序,根據https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android

AdView adView = (AdView)this.findViewById(R.id.adView); 
    adView.loadAd(new AdRequest()); 

編輯2:

與往常一樣,您必須用您的AdMob發佈商ID替換MY_AD_UNIT_ID。 您還必須在廣告中添加您自己的設備ID:testDevices屬性 以在您的設備上獲取測試廣告。

我只是假設你實際上正在改變這些值...是的,你必須真正把這些值。

+0

它仍然沒有運行。它打開一個小盒子和盒子裏的按鈕,但沒有別的。 – user2727048

+0

您加載了廣告嗎?除了xml部分外,您還需要在活動中執行一些代碼。該Admob開發指南有一個教程 –

+0

我被告知,並看到了幾個例子,是廣告只是在XML中創建的,我沒有任何代碼在活動發起廣告 – user2727048

-1

只需設置頁邊距0dp水庫 - >值 - > dimens.xml

<dimen name="activity_horizontal_margin">0dp</dimen> 
<dimen name="activity_vertical_margin">0dp</dimen> 
相關問題