【问题标题】:Add a view at the end of scrolling webview Android在滚动webview Android的末尾添加一个视图
【发布时间】:2020-01-20 11:38:36
【问题描述】:

我想在 webview 的滚动末尾添加一个自定义视图,所以当用户到达 webview 的末尾时,他可以看到自定义视图。 如何在 Android 中实现这一目标?

  <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fillViewport="true">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

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

                <com.app.util.CustomView
                    android:visibility="visible"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>

我尝试将自定义视图放在底部和滚动视图内,但效果不佳,webview 和我的自定义视图之间有很大的空白区域

编辑

我尝试了嵌套滚动但仍然无法正常工作,对于这个网址“https://vimeo.com/171614342#embed”,它看起来像这样

我看不到页脚或 cmets 部分。 这是嵌套滚动的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:padding="10dp"
            android:background="@android:color/holo_red_dark"
            android:text="Hello World!" />
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

【问题讨论】:

  • 如果你添加任何你尝试过的相关sn-ps就可以了。它可能会帮助 stackoverflow 用户解决您的问题
  • 在下面查看我的答案
  • 拥有滚动视图的父布局是什么?
  • NestedScrollView 是父级,它是片段的布局,包含这个片段的活动有 FrameLayout 持有片段
  • 顶部的工具栏怎么来的?可能您需要将活动扩展到 AppCompatActivity

标签: android android-studio webview android-webview


【解决方案1】:

将组件包装在 NestedScrollView 而不是 ScrollView 中。参考下面的sn-p。

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:padding="10dp"
            android:background="@android:color/holo_red_dark"
            android:text="Hello World!" />
    </LinearLayout>

</androidx.core.widget.NestedScrollView>

同时查看截图。

希望你有解决方案..

【讨论】:

  • 不幸的是,这并没有解决问题,我可以看到 webview 和自定义视图之间的空白,就像我附加的屏幕截图一样
  • 去掉你的 weight 属性和 fillviewport 试试?
  • 我做到了,我认为问题出在高度 = wrap_content 的 webview 中,当我将其更改为“match_parent”时,我可以完全看到 webview,但是自定义视图被隐藏了
  • 您能否将我的代码复制粘贴到单独的活动中并检查它是否有效?因为您应用于活动的主题可能存在问题
  • 没用,我编辑了我的问题以包含您的代码和附加的屏幕截图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 2020-11-15
  • 1970-01-01
  • 2017-01-28
相关资源
最近更新 更多