如何在网络视图下方放置广告

How to place an ad below the web view

我想在网络视图下方放置 Facebook Audience 网络横幅广告。我不希望广告与我的网络视图重叠。这几天我都没能实现。

这是我的布局文件,我在约束布局中将广告放在 webview 之后

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/web_view_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <View
        android:id="@+id/divider"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="?android:attr/listDivider"
        app:layout_constraintTop_toTopOf="parent" />

    <WebView
        android:id="@+id/web_view_documentation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/native_banner_ad_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

我在 webview 上方放置了一个 AppBarLayout。如果我添加约束,我的 webview 开始与应用栏重叠。

我的网页视图高度和宽度设置为 match_parent。我希望我的 webview 在广告后保留完整的 space。所以,基本上,如果我的广告高度为 100dp,webview 应该填充剩余的 space 而不会重叠或欠重叠。提前致谢。

您可以将视图置于垂直链中,这样当广告的 RelativeRelayout 容器的高度设置为 [=13= 时,WebView 占据所有剩余的 space ]:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/web_view_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <View
        android:id="@+id/divider"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="?android:attr/listDivider"
        app:layout_constraintBottom_toTopOf="@id/web_view_documentation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <WebView
        android:id="@+id/web_view_documentation"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/native_banner_ad_container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/divider"/>

    <RelativeLayout
        android:id="@+id/native_banner_ad_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/web_view_documentation"/>

</android.support.constraint.ConstraintLayout>

只需将布局文件替换为以下内容即可。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <WebView
        android:id="@+id/web_view_documentation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_above="@id/native_banner_ad_container"/>

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