【问题标题】:Displaying a View beneath a WebView在 WebView 下显示 View
【发布时间】:2014-08-15 19:06:54
【问题描述】:

如果这个问题已经得到解答,我深表歉意,广泛的谷歌搜索发现我很多类似但不完全相同的情况没有提供答案。我有一个 WebView,想在它下面放一个 Google AdView。但是,我不希望添加始终附加到窗口底部,我希望它出现在 WebView 之后,即我滚动浏览网页的内容,然后在其下方显示广告。我已经尝试了许多相对和线性布局的变体,这里以一种尝试为例:

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_weight="1"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/web_frag_ad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="myUnitId"
        ads:adSize="BANNER" />

</LinearLayout>

我尝试过使用 layout_below 的 RelativeLayout,但 WebView 将其覆盖了。如果我 alignParentBottom 会显示,但这不是我想要的行为。我希望它仅在滚动浏览网页内容后才可见。我确信我犯了一个简单的布局错误,非常感谢帮助我指出正确的方向。

【问题讨论】:

  • 我怀疑这将很难实现。我认为您必须跟踪WebView 中的所有滚动移动,然后在用户向底部滚动时在广告中设置动画。如果WebView 中的Web 内容是您自己的内容,那么在HTML 中嵌入广告横幅会简单得多。
  • 同意,这将很难实现。您必须监视 webview 滚动并具有在滚动后启动 web_frag_ad 的功能。我建议您将广告放在 HTML 中,就像 @CommonsWare 建议的那样。

标签: android android-layout webview


【解决方案1】:

我已经做过类似的事情了:

1- 像这样构建你的布局:

<LinearLayout
    ... >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_weight="1"/>

        <com.google.android.gms.ads.AdView
            android:id="@+id/web_frag_ad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adUnitId="myUnitId"
            ads:adSize="BANNER" />
    </ScrollView>
</LinearLayout>

2- 在内容呈现后计算 Web 视图的内容高度。如果你不知道怎么做,告诉我,我可以给你相应的代码。

3- 设置 webView.height = content.height

所以你的 scrollview.content 会调整到全局高度 webView.content+web.frag_ad.height

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多