简体   繁体   中英

Android - Smart Banner - width not sufficient (always one pixel missing)

I want to add a SMART_BANNER at the bottom of my Android App (HTML5), the layout is defined as follows

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="0dp" android:paddingRight="0dp" android:background="@color/default_header" > <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:background="@color/add_background" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="0dp" android:paddingRight="0dp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="SMART_BANNER" ads:adUnitId="@string/ad_banner"> </com.google.android.gms.ads.AdView> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/adView" android:background="@color/start_background" android:id="@+id/webView" /> 

It works perfectly fine with "BANNER" but in case of a "SMART_BANNER" always with is always one pixel to small and the following error is shown:

W/Ads: Not enough space to show ad. Needs 412x90 dp, but only has 411x750 dp. W/Ads: Not enough space to show ad. Needs 412x90 dp, but only has 411x750 dp.

Question: Where did I lose the one dp (from 412 to 411)?

Add info: I am using an emulator (from Android Studio). Using issue occurs if I use a 'Pixel 2XL API 26' (SMART banner not shown), but the banner is correctly shown if I use a 'Pixel API 28'

Best regards Andreas

You need to set the AdView's width to wrap_content instead of match_parent . At least that's what I do with my AdViews and it always works regardless if it's a BANNER or SMART_BANNER.

[Off-topic] I've also noticed that on your WebView you wrote android:layout_above="@+id/adView" . In this case you don't need to declare a new reference to adView since it's already been declared before. So if you simply put @id/adView instead of @+id it will still work.

Also try removing those paddings (even though it's set to 0) both from your layout and the AdView. There is no need for them from what I see.

you should set width and height like this :

  <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/admob_unitid_web">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM