android - 如何使网页 View 与其内容一样大?

标签 android webview horizontalscrollview

我在网上找不到这个。 我在 Horizo​​ntalScrollView 中有很多 webview,我希望它们都适合它的内容(然后可能有不同的宽度)。 此外,我像这样膨胀那些 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);

“页面持有者”布局:

<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>

然后是“ 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>

最佳答案

wrap_content 和 WebView 的问题在于网页的呈现与 View 层次结构中的布局不同步。当 Android 测量 webviews 的大小以确定将它们放置在 LinearLayout 中的位置时,页面可能尚未呈现,因此宽度为 0。您需要为每个 WebView 添加一个 PictureListener 并确定何时所有 onNewPicture( WebView, Picture) 回调已被调用。然后,您可以在 LinearLayout 上调用 requestLayout() 来安排布局过程。

关于android - 如何使网页 View 与其内容一样大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14519721/

相关文章:

android - CSS 不适用于 WebView.loadDataWithBaseURL

android - 如何使用Android Horizo​​ntalScrollView 将图片一张一张移动

android - notifyItemRangeChanged() 导致 RecyclerView Android 崩溃

android - Facebook 时间线,如 ListView

在纵向和横向之间切换时,Android整数xml资源值不会改变

android - 如何创建带有圆角的 EditText?

java - 在 WebView 中使用 GOOGLE TRANSLATE。 "Desktop"工作, "Mobile"不工作

Android:在页面加载时显示临时图像

Android 在 vi​​ewpager 中点击转到第一个 fragment 后返回同一个 fragment

android - 使用 camera2 api 在前置摄像头中全屏录制视频