如何让网页视图一样大,它的内容? [英] How to make webviews as large as its content?

查看:169
本文介绍了如何让网页视图一样大,它的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在网络上找到。
我有一个 Horizo​​ntalScrollView ,我希望所有的人都适合其内容(当时可能有不同的宽度)网页视图很多。
更多的,我吹那些网​​页视图是这样的:

 的LinearLayout布局=(的LinearLayout)getLayoutInflater()膨胀(R.layout.webview_holder,NULL);
web视图=(的WebView)layout.findViewById(R.id.webView);
webView.loadData(数据,text / html的;字符集= UTF-8,UTF-8);
。webView.getSettings()setUseWideViewPort(真);
webView.setScrollbarFadingEnabled(真);
pageHolder.addView(布局);

页面持有人布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    < Horizo​​ntalScrollView
    机器人:ID =@ + ID /滚动视图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fillViewport =真
    机器人:方向=横向>    <的LinearLayout
        机器人:ID =@ + ID / page_holder
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:方向=横向>
    < / LinearLayout中>
    < / Horizo​​ntalScrollView>
< / LinearLayout中>

,然后在布局的WebView

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =match_parent>
    <的WebView
    机器人:ID =@ + ID / web视图
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =match_parent/>
< / LinearLayout中>


解决方案

WRAP_CONTENT 的问题,网页视图是网页的渲染是异步从视图中的布点层次结构。当Android的测量网页视图的大小来确定在何处放置他们在的LinearLayout,页面可能没有被渲染,因此宽度为0。你需要一个PictureListener添加到每个网页视图,并确定所有的 onNewPicture(的WebView,资料图片)回调已被调用。然后,你可以调用 requestLayout()您的LinearLayout来安排布局传递。

I couldn't find this on the web. I have many webviews in a HorizontalScrollView, I want all of them fit its content (then potentially have a different width ). More, I inflate those webviews like this:

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

The "page holder" 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>

and then the "webview layout" :

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

解决方案

The problem with wrap_content and WebViews is that the rendering of the webpage is async from layouting in the view hierarchy. When Android measures the size of the webviews to determine where to place them in the LinearLayout, the pages may not have been rendered and hence the width is 0. You need to add a PictureListener to each WebView and determine when all onNewPicture(WebView, Picture) callbacks have been called. Then you can call requestLayout() on your LinearLayout to schedule a layout pass.

这篇关于如何让网页视图一样大,它的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆