【问题标题】:Android webview content sometimes not fit the widthAndroid webview 内容有时不适合宽度
【发布时间】:2014-12-24 03:56:20
【问题描述】:

在我的应用程序中,

首先我有一些 html 代码作为字符串,然后我将它加载到 webview 中,如下所示:

WebView content = (WebView) rootView.findViewById(R.id.content);

StringBuilder sb = new StringBuilder();
        sb.append("<HTML><HEAD><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'></HEAD><body>");
        sb.append(pageItem.page_content_chi.toString());
        sb.append("</body></HTML>");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            content.getSettings().setAllowUniversalAccessFromFileURLs(true);
            content.getSettings().setAllowFileAccessFromFileURLs(true);
        }

        content.setWebChromeClient(new WebChromeClient());
        content.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        });

        content.getSettings().setUseWideViewPort(true);
        content.getSettings().setJavaScriptEnabled(true);
        content.getSettings().setLoadWithOverviewMode(true);
        content.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String cachePath = ctx.getCacheDir().getAbsolutePath();
        content.getSettings().setAppCachePath(cachePath);
        content.getSettings().setAppCacheEnabled(true);
        content.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        content.loadDataWithBaseURL("file:///android_asset/",sb.toString(), "text/html", "utf-8", null);
        content.setBackgroundColor(0x00000000);

        content.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return (event.getAction() == MotionEvent.ACTION_MOVE);
            }
        });

        content.setVerticalScrollBarEnabled(false);
        content.setHorizontalScrollBarEnabled(false); 

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="vertical" >

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

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_margin="5dp"
                android:textSize="25sp"
                android:textStyle="bold" />

            <WebView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

问题是,webview 中的内容有时适合宽度,但有时不适合并超过宽度。它是随机发生的,所以当我单击选项卡并重新加载 webview 时,它有时会适合视图,有时会超出视图。如何解决这个问题?非常感谢

【问题讨论】:

  • 这可能是因为您使用了width=device-width,通常网络和移动设备的行为会发生变化。尝试改变它。

标签: android android-layout android-activity webview android-websettings


【解决方案1】:

将 WebView 属性用作

<WebView
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

完成此操作后,您必须看到的下一件事是,您尝试在 web 视图中获取内容的网站是否具有响应性,这意味着它是否以清晰和精细的分辨率在所有屏幕上工作.如果不是,那么您必须使网页在 Webview 中响应您,例如

@media (min:320px , max:568px ){ Css Body}

【讨论】:

  • fill_parent 现在可以工作,但它没有响应,我如何确定每个手机都有它的 px 的大小?谢谢
  • 当您使用 CSS 文件时,您必须使网站具有响应性,我认为总共有 7 个屏幕,或者如果网络专门用于移动设备,则只有 4 个屏幕。如果您以百分比创建网站,那么在一个小时内制作 4 个屏幕非常容易。否则我会在 px 中花费超过 2 个小时
  • 您必须检查手机的响应屏幕
【解决方案2】:

请在清单文件中添加以下代码:

<supports-screens android:smallScreens="true"
    android:normalScreens="true" android:largeScreens="true"
    android:anyDensity="true" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-08
    • 2022-09-23
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多