adView не отображается с содержимым переноса - PullRequest
1 голос
/ 28 апреля 2019

В макете XML все кажется идеальным.Когда я использовал эмулятор или реальное устройство, adView не видно.Я пытался дать высоту adView, как 200 dp.Тогда adView виден.Но когда я дал значение wrap_content для высоты adView, возникла проблема.Я использовал LinearLayout и RelativeLayout, но проблема та же.Что я должен сделать для этого?

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="30dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="@string/nav_contact_us_content_explanation" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="email"
            android:gravity="center"
            android:selectAllOnFocus="false"
            android:text="@string/nav_contact_us_content_mail" />
    </LinearLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adview_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
</LinearLayout>

Ответы [ 2 ]

0 голосов
/ 28 апреля 2019

В случае LinearLayout, поскольку вес был установлен на 1, он занимал весь экран, когда объявление было еще не загружено . Тем не менее, вы должны увидеть предупреждение, подобное следующему в вашем logcat здесь.

W/Ads: Not enough space to show ad. Needs 320x50 dp, but only has 288x495 dp.

Чтобы решить эту проблему, вы можете использовать RelativeLayout вместо LinearLayout, как показано ниже.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adview"
        android:orientation="vertical"
        android:padding="30dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="Contact us" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="email"
            android:gravity="center"
            android:selectAllOnFocus="false"
            android:text="Email" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/adview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adview_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />
    </LinearLayout>
</RelativeLayout>

С другой стороны, вы можете также рассмотреть возможность установки фиксированного размера для AdView, чтобы он не скрывался, когда содержимое еще не загружено. Вы можете получить размеры разных типов рекламы для разных размеров экрана здесь .

0 голосов
/ 28 апреля 2019

Попробуйте поместить AdView в FrameLayout

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="30dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:text="@string/nav_contact_us_content_explanation" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="email"
            android:gravity="center"
            android:selectAllOnFocus="false"
            android:text="@string/nav_contact_us_content_mail" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adview_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>

    </FrameLayout>
</LinearLayout>
...