2014-02-16 71 views
0

我正在將我的應用定位到手機和平板電腦。我在/layout/layout-land上創建了fragments.xml廣告未在片段上顯示

在管理我已經把這個片段的類:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragments); 

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

    if (findViewById(R.id.fragmentsboth) != null) { 
     numberOfPanes = 1; 
     if (savedInstanceState != null) { 
      return; 
     } 
     QueryFragment firstFragment = new QueryFragment(); 
     getSupportFragmentManager().beginTransaction().add(R.id.fragmentsboth, firstFragment).commit(); 
    } else if (savedInstanceState == null) { 
     QueryFragment firstFragment = new QueryFragment(); 
     getSupportFragmentManager().beginTransaction().add(R.id.fragment_one, firstFragment).commit(); 
     if (operation != 5) { 
      Infodata secondFragment = new Infodata(); 
      getSupportFragmentManager().beginTransaction().add(R.id.fragment_two, secondFragment).commit(); 
     } else { 
      Compare secondFragment = new Compare(); 
      getSupportFragmentManager().beginTransaction().add(R.id.fragment_two, secondFragment).commit(); 
     } 
    } 
    } 

我已經添加了AdView到我的兩個fragments.xml的,它是按預期運行在手機上。該廣告是展示。

在平板電腦上,我可以看到廣告的空間,但沒有添加顯示。

下面是從fragments.xml代碼爲片劑:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/lightgrey"> 
    <LinearLayout 
      android:id="@+id/fragment_container" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/lightgrey" 
      android:baselineAligned="false"> 
     <LinearLayout 
       android:id="@+id/fragment_one" 
       android:orientation="horizontal" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="fill_parent" 
       android:background="@color/lightgrey"> 
     </LinearLayout> 
     <LinearLayout 
       android:id="@+id/LinearLayout01" 
       android:layout_width="0dp" 
       android:layout_weight="2" 
       android:layout_height="fill_parent" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:baselineAligned="false"> 
      <LinearLayout 
        android:id="@+id/fragment_two" 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:background="@color/lightgrey" 
        android:layout_weight="1"> 

      </LinearLayout> 
      <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"> 
       <com.google.android.gms.ads.AdView 
         android:id="@+id/adView" 
         xmlns:ads="http://schemas.android.com/apk/res-auto" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         ads:adUnitId="12121212" 
         ads:adSize="SMART_BANNER" 
         android:layout_gravity="center|bottom" 
         android:layout_alignParentStart="true"/> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 
    <ListView 
      android:id="@+id/list_slidermenu" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:choiceMode="singleChoice" 
      android:divider="@color/orange" 
      android:dividerHeight="1dp" 
      android:listSelector="@drawable/menulist_selector" 
      android:background="@color/lblack"/> 
</android.support.v4.widget.DrawerLayout> 

爲什麼沒有顯示廣告?

+0

LogCat顯示一些關於廣告空間的警告? – StarsSky

+0

是的:)你說得對。沒有添加空間。這能解決嗎? '沒有足夠的空間顯示廣告。需要800x119,但只有534x1108' – Favolas

回答

2

當logcat的顯示是這樣的:

WARN/Ads(3805): Not enough space to show ad! Wants: <xxx, xx>, Has: <xxx, xx>. 

這意味着該廣告沒有被顯示空間。你的Layout佔據了視野的所有高度。所以即使你在你的layot中指定了AdView,它也會變成0高度。

+0

謝謝。刪除了'SMART_BANNER'並用'BANNER'替代,現在它可以工作。一個問題,但。如果我從網絡上斷開連接,廣告空間仍然存在。如何更改我的佈局,以便在有網絡的情況下顯示廣告時不會與任何內容重疊且沒有網絡,「@ + id/LinearLayout01」或「@ + id/fragment_two」佔據屏幕的所有高度? – Favolas

+1

您應該實施adlistener和onAdFailedToLoad方法:https://developer.android.com/reference/com/google/android/gms/ads/AdListener.html#onAdFailedToLoad(int)如果您收到錯誤消息,請從用戶界面(刪除或隱藏與知名度。隱藏 – StarsSky

+0

將盡力。非常感謝:) – Favolas