Android 在网络视图的顶部和底部显示adview?

Android 在网络视图的顶部和底部显示adview?,android,android-layout,android-xml,Android,Android Layout,Android Xml,我有一个网络视图,我想在页面的顶部和底部放置一个adview: 忠告 网络视图 忠告 我试过: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:ori

我有一个网络视图,我想在页面的顶部和底部放置一个adview:

忠告

网络视图

忠告

我试过:

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

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-1/1">
    </com.google.android.gms.ads.AdView>

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-1/1">
        </com.google.android.gms.ads.AdView>

    </LinearLayout>

顶部的adview看起来是正确的,但底部的横幅没有显示出来。
有什么想法吗?

您可以使用
layout\u weight
属性,如下所示

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

您的网络视图覆盖了底部的adview。 在WebView标记中

android:layout_height="fill_parent"
换成

android:layout_height="wrap_content"
如果你仍然想覆盖整个页面,那么你至少需要为adview留出一些空间

android:layout_height="fill_parent"
android:layout_marginBottom="90dp"

现在是困难的部分,在测试中,您将得到一个90dp的广告,但在实时情况下,您永远不知道它的大小,因此命名为smart banner(高度可以变化)

尝试使用约束布局,如下所示

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-1/1"
    ads:layout_constraintEnd_toEndOf="parent"
    ads:layout_constraintStart_toStartOf="parent"
    ads:layout_constraintTop_toTopOf="parent">
</com.google.android.gms.ads.AdView>

<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/adView2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/adView"/>

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-1/1"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintEnd_toEndOf="parent"
    ads:layout_constraintStart_toStartOf="parent">
</com.google.android.gms.ads.AdView>


底部横幅未显示,因为您设置了webview填充的高度

 <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

我希望它对您有用

使用布局权重将webview设置为1并将高度更改为0dp。您是否找到了基于智能横幅高度对齐视图的解决方案?
  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_weight=".1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-1/1">
    </com.google.android.gms.ads.AdView>

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webView1"
        android:layout_weight=".8"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        />

        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:layout_weight=".1"
            android:id="@+id/adView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-1/1">
        </com.google.android.gms.ads.AdView>

    </LinearLayout>