包装内容不可见adView

时间:2019-04-27 21:07:06

标签: java android adview

在XML布局中,一切看起来都很完美。当我使用仿真器或真实设备时,adView不可见。我试图像adView那样给200 dp增高。然后adView可见。但是,当我给出wrap_content的高度的值adView时,就会出现问题。我使用了LinearLayoutRelativeLayout,但是问题是相同的。我该怎么办?

<?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 :(得分:0)

尝试将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>

答案 1 :(得分:0)

对于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设置一个固定大小,以便在尚未加载内容时它不会隐藏。您可以针对不同的屏幕尺寸here获取不同类型的广告的尺寸。