2014-02-16 69 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