如何使webview与其内容一样大?

use*_*718 3 android webview horizontalscrollview

我在网上找不到这个.我在a中有很多webview HorizontalScrollView,我希望它们都适合它的内容(然后可能有不同的宽度).更多,我夸大webviews这样的:

LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.webview_holder, null);
webView =  (WebView) layout.findViewById(R.id.webView);
webView.loadData(data, "text/html; charset=UTF-8", "UTF-8");
webView.getSettings().setUseWideViewPort(true);
webView.setScrollbarFadingEnabled(true);
pageHolder.addView(layout);
Run Code Online (Sandbox Code Playgroud)

"页面持有者" layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/page_holder"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后是"webview布局":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <WebView 
    android:id="@+id/webView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Rod*_*dja 6

wrap_content和WebViews 的问题在于,网页的呈现与视图层次结构中的布局是异步的.当Android测量web视图的大小以确定将它们放置在LinearLayout中的位置时,页面可能尚未呈现,因此宽度为0.您需要向每个WebView添加PictureListener并确定何时onNewPicture(WebView, Picture)调用所有回调.然后,您可以调用requestLayout()LinearLayout来安排布局传递.

  • PictureListener已被弃用.您可以改为设置WebViewClient并覆盖onPageFinished()方法 (3认同)