android - 如何在 WebView 下方放置广告

标签 android scroll android-webview android-constraintlayout facebook-audience-network

我想在 WebView 下方放置一个 Facebook Audience 网络横幅广告。我不希望广告与我的 WebView 重叠。我已经好几天没能实现了。

这是我的布局文件,我在约束布局中将广告放在 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 开始与应用栏重叠。

我的 webview 高度和宽度设置为 match_parent。我希望我的 WebView 占据广告后剩余的全部空间。所以,基本上,如果我的广告高度为 100dp,webview 应该填充剩余空间而不会重叠或欠重叠。提前致谢。

最佳答案

您可以将 View 置于垂直链中,这样当广告的 RelativeRelayout 容器的高度设置为 wrap_content 时,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_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>

关于android - 如何在 WebView 下方放置广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458514/

相关文章:

jquery - 视差滚动部分的高度不固定/100%?

javascript - 在 android WebView 中的 java 和 javascript 之间共享一个对象(数据)

android - 如何监听 Android Webview 中的任何 cookie 更改

javascript - 如何在单击jQuery时向下滚动一定距离

javascript - 如何禁用外部元素的滚动?

android - Android 上的 Chromium webview 和 Chrome 应用程序之间的字体垂直对齐差异

Android ViewFlipper 使用来自 URL 的图像更改背景如何

Android RTSP 直播问题

Android - 谷歌地图 - LocationListener - 添加标记 onLocationChanged 使应用程序崩溃

java - 回收器 View 需要 30 秒才能加载,并且滚动时也会滞后