【问题标题】:How to add webview scroll in an scrollView如何在scrollView中添加webview滚动
【发布时间】:2019-08-07 14:30:58
【问题描述】:

我需要在一个列表中显示多个项目并在最后添加一个 WebView。

我添加了一个 RecyclerView 和一个 WebView,并将它们都放在了一个 ScrollView 中。 这工作正常,但它没有在 webview 中显示滚动。

我需要我的 webview 有一个垂直滚动。

这是我的代码。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Title"
    >


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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvMatchday"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <WebView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_gravity="bottom"
            android:scrollbars="vertical"
            android:background="@color/black"
            android:id="@+id/twitterWebView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rvMatchday" />


    </LinearLayout>

</ScrollView>

然后我创建了一个父布局文件,并在其中添加了上面的布局。

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

   <include layout="@layout/fragment_match"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</ScrollView>

【问题讨论】:

  • 你尝试使用NestedScrollView吗?

标签: android android-layout android-fragments android-intent


【解决方案1】:

我认为您不需要以 root 身份使用滚动视图,因为回收站视图和 Web 视图具有自己的滚动效果。只需尝试以线性为根

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvMatchday"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/black"
        android:id="@+id/twitterWebView"
       />


</LinearLayout>

【讨论】:

    【解决方案2】:

    您不需要在 layout.xml 文件和父布局文件中使用滚动视图。 喜欢

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvMatchday"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <WebView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_gravity="bottom"
                android:scrollbars="vertical"
                android:background="@android:color/black"
                android:id="@+id/twitterWebView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rvMatchday" />
                 </LinearLayout>
    

    和父布局

        <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <include layout="@layout/fragment_match"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      在您的活动/片段上,将您的 webview 设置为可滚动

      webView.setVerticalScrollBarEnabled(true);
      

      【讨论】:

        猜你喜欢
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多