我在网上找不到这个。
我在 HorizontalScrollView
中有很多 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/