2013-08-01 65 views
4

我的肖像應用程序底部有一個智能橫幅。如何使admob中的SMART_BANNER廣告在Android中正確填充?

我的佈局是這樣的:

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingBottom="0dp" 
     android:paddingLeft="0dp" 
     android:paddingRight="0dp" 
     android:paddingTop="0dp"> 

     <TextSwitcher 
      ... 
      Some TextSwitcher Stuff Here 
      ... /> 

     <com.google.ads.AdView 
      xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads" 
      android:id="@+id/bannerAd" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:gravity="bottom" 
      android:visibility="visible" 
      googleads:adSize="SMART_BANNER" 
      googleads:adUnitId="@string/admobId" /> 

    </RelativeLayout> 

我的清單具有相應權限,這樣的:

<activity 
      android:name="com.google.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" > 
</activity> 

我的廣告是由該代碼初始化:

private void initialiseAds() 
    { 
     AdView adView = (AdView) findViewById(R.id.bannerAd); 
     adView.setAdListener(new MyAdListener(adView)); 
     AdRequest adRequest = new AdRequest(); 
     adRequest.addKeyword("games"); 
     adRequest.addKeyword("tabletop games"); 
     adRequest.addKeyword("board games"); 
     adRequest.addKeyword("monopoly"); 
     adRequest.addKeyword("gambling"); 
     adRequest.addKeyword("dice"); 
     final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); 
     String deviceId = tm.getDeviceId(); 
     adRequest.addTestDevice(deviceId); 
     adView.loadAd(adRequest); 
    } 

當我運行應用程序,我的廣告不會顯示。 LogCat給我這個:

08-01 11:24:59.015: E/Ads(10436): Not enough space to show ad! Wants: <720, 100>, Has: <656, 935> 
08-01 11:24:59.020: E/Ads(10436): Not enough space to show ad! Wants: <720, 100>, Has: <656, 935> 

該設備是一個銀河S3。這似乎是錯誤的大小((720,100)肯定對於手機橫幅應用程序太大)。

SMART_BANNER是用XML聲明的,所以我不能相信AdView需要重新生成incode,因爲它必須已經知道第一個實例的大小了嗎?

任何想法?

編輯:

如果我把SMART_BANNER只有最外部的佈局裏面,這是XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/btnRollDice" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:layout_marginLeft="@dimen/activity_horizontal_margin" 
     android:layout_marginRight="@dimen/activity_horizontal_margin" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:contentDescription="@string/buttonDescription" 
     android:onClick="rollDice" 
     android:text="@string/btnRoll" 
     android:textSize="40sp" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingBottom="0dp" 
     android:paddingLeft="0dp" 
     android:paddingRight="0dp" 
     android:paddingTop="0dp"> 

     <TextSwitcher 
      android:id="@+id/diceValue" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center" 
      android:animateFirstView="false" 
      android:contentDescription="@string/textSwitcherDescription" 
      android:inAnimation="@anim/slide_down" 
      android:outAnimation="@anim/abc_fade_out" 
      android:textAlignment="center" 
      android:visibility="visible" /> 

    </RelativeLayout> 
    <com.google.ads.AdView 
      android:id="@+id/bannerAd" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_margin="0dp" 
      ads:adSize="SMART_BANNER" 
      ads:adUnitId="@string/admobId" 
      android:gravity="bottom" 
      android:padding="0dp" 
      android:visibility="visible" /> 
</LinearLayout> 

這是錯誤消息:

08-01 12:59:38.120: E/Ads(22919): Not enough space to show ad! Wants: <720, 100>, Has: <720, 0> 

我懷疑這是因爲RelativeLayout和Button已經填充了LinearLayout並且沒有留下AdView的空間。有沒有辦法將wrap_contents的高度值分配給adview而不影響RelativeLayout?

編輯:解:

這得到它的方式約95%有(足夠好:))。 TextSwitcher約有1%的折扣死機,它的高度略高,但除非你凝視並比較數小時,否則你不會注意到。希望這會幫助某人。感謝Harshid和William的貢獻。

<!-- This is the XML for the Portrait View of the App 
It has 2 major tiers 
The first tier contains 3 items: 
    The Ad Banner 
    The button 
    The 2nd Tier 
The second tier contains 1 item 
    The TextSwitcher 

XML is pretty readable so just read it! :) 

--> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    tools:context=".MainActivity" 
    android:id="@+id/masterContainer"> 


    <com.google.ads.AdView 
     android:id="@+id/bannerAd" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_margin="0dp" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     ads:adSize="SMART_BANNER" 
     ads:adUnitId="@string/admobId" 
     android:padding="0dp" 
     android:visibility="visible" /> 

    <Button 
     android:id="@+id/btnRollDice" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:layout_marginLeft="@dimen/activity_horizontal_margin" 
     android:layout_marginRight="@dimen/activity_horizontal_margin" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:layout_marginBottom="0dp" 
     android:paddingBottom="0dp" 
     android:contentDescription="@string/buttonDescription" 
     android:onClick="rollDice" 
     android:text="@string/btnRoll" 
     android:textSize="40sp" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@id/bannerAd" 
     android:layout_below="@id/btnRollDice" 
     android:layout_margin="0dp" 
     android:orientation="vertical" 
     android:paddingBottom="0dp" 
     android:paddingLeft="0dp" 
     android:paddingRight="0dp" 
     android:paddingTop="0dp" > 

      <TextSwitcher 
       android:id="@+id/diceValue" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="false" 
       android:layout_alignParentLeft="false" 
       android:layout_alignParentRight="false" 
       android:layout_alignParentTop="false" 
       android:layout_centerHorizontal="false" 
       android:layout_centerInParent="true" 
       android:layout_centerVertical="false" 
       android:layout_gravity="center" 
       android:animateFirstView="false" 
       android:contentDescription="@string/textSwitcherDescription" 
       android:inAnimation="@anim/slide_down" 
       android:outAnimation="@anim/abc_fade_out" 
       android:textAlignment="center" 
       android:visibility="visible" /> 

     </RelativeLayout> 

</RelativeLayout> 

回答

4

我覺得你的Admob banner是在佈局。

看跌旗幟的所有佈局和內的主要佈局。因爲你已經使用的填充,保證金等等等等的側..

<Your Main Layout> 
    <Secondary Layout> 
     < Layout /> 
    </Secondary Layout> 
//put your banner here 
</Your Main Layout> 

嘗試this方式。

+0

更新的問題與新的XML文件:) – BigTobster

+0

更好,但仍不完全。橫幅現在顯示完美(做得好!),但容器是wrap_content,所以它填充頁面的其餘部分(因爲smart_banner可以是任何高度)。這意味着內容被推出頁面的中心。我能想到的唯一方法是回到方形1並將兩個元素放在一個室內佈局中! – BigTobster

+0

不幸的是,您的答案不適用於我的問題。我試過調整它,但它嚴重不會一起去(我是一個noob)。我會接受你的回答,理由是它可能適用於沒有與我做同樣設置的其他人。謝謝你的幫助。 – BigTobster

0

檢查設備DP和負載使用各種廣告常數(橫幅,排行榜等)與AdMob中介給出相應的鍵。

+0

我沒有使用中介。只需使用標準廣告。我在我的設備DP上特別檢查什麼? – BigTobster

1

如果你想使用你的第二個佈局,那麼你應該給相對佈局layout_weight 1(layout_weight只適用於LinearLayouts)。這將告訴它擴展以填充包含LinearLayout中的任何未使用的空間。

建議您閱讀http://developer.android.com/guide/topics/ui/declaring-layout.html以更好地理解Android佈局,您無差別地使用RelativeLayout和LinearLayout的屬性。某些佈局屬性僅適用於一種佈局。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    ...> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     .../> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     ...> 

     <TextSwitcher 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      .../> 
    </RelativeLayout> 

    <com.google.ads.AdView 
      android:id="@+id/bannerAd" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      ads:adSize="SMART_BANNER" 
      ads:adUnitId="@string/admobId" 
    /> 
</LinearLayout> 
+0

感謝您的回答。這非常有幫助。你的鏈接已經給了我最終需要的代碼所需的答案:)一些狡猾的屬性是因爲我改變了相對和線性佈局並且該死的懶惰!其中一些我搞砸了,所以這個鏈接絕對有用。 – BigTobster

6

確保您的外部佈局沒有任何填充

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" // Change to 0dp 
    android:paddingRight="0dp" // Change to 0dp 
    android:paddingTop="2dp" > 
0

從你的XML代碼,刪除四個填充屬性即paddingBottom來,PaddingLeft,PaddingRight,PaddingTop