【问题标题】:WebView with only an image doesn't fit in screen只有图像的 WebView 不适合屏幕
【发布时间】:2014-04-14 06:24:15
【问题描述】:

我正在为我的网站使用 Rss 阅读器,我得到了包含文本和图像的 <description> 标签。为了毫无问题地使用它们,我将文本和图像分成两个字符串。我正在使用 webview 加载图像,因为我创建的图像字符串包含 <img src> 标记。

一切正常,但在我在模拟器中的测试中,图像在高达 720p 的屏幕中正常显示。例如,当我为 Nexus 5 使用 1080p 的模拟器时,图像不会缩放到屏幕,并且我会在右侧看到一个滚动条。

要理解这里的问题是 720p 屏幕中的图像: http://imgur.com/VArmsmb 和 1080p 的图像: http://imgur.com/7zGkX0J

我正在显示所有这些的片段的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/sv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/title"
        android:layout_marginTop="10dp" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <WebView
                android:id="@+id/imgWebView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbarStyle="insideOverlay"
                android:text="@string/desc" />

            <WebView
                android:id="@+id/textWebView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imgWebView"
                android:scrollbarStyle="insideOverlay"
                android:text="" />

            <WebView
                android:id="@+id/fullWebView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbarStyle="insideOverlay"
                android:text=""
                android:visibility="gone" />

            <Button
                android:id="@+id/readMore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textWebView"
                android:layout_centerHorizontal="true"
                android:text="@string/fullArticle" />
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

这个特定网页视图的代码是这样的:

final WebView imgW = (WebView) view.findViewById(R.id.imgWebView);
WebSettings ws = imgW.getSettings();
        ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        ws.setLightTouchEnabled(false);
        ws.setPluginState(PluginState.ON);
        ws.setJavaScriptEnabled(true);
        ws.setLoadWithOverviewMode(true);
        ws.setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

imgW.loadDataWithBaseURL("http://www.liveplace.gr/", imgCode, "text/html", "UTF-8", null);

知道为什么会发生这种情况吗?

【问题讨论】:

    标签: android image webview resize


    【解决方案1】:

    希望对您有所帮助:

    以下几行将根据屏幕尺寸固定尺寸...

    WebSettings settings = yourWebView.getSettings();
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);
    

    其他方式请参考This Link...

    【讨论】:

    • 似乎可以通过在img标签前添加:&lt;head&gt; &lt;meta name="viewport" content="width=device-width, user-scalable=no" /&gt; &lt;/head&gt;来修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多