在Android的web视图使用元宽视 [英] Use meta viewport width in Android Webview

查看:206
本文介绍了在Android的web视图使用元宽视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用视口标签,以适应HTML内容到一个web视图。

I would like to use the viewport tag to fit html content into a WebView.

<meta name='viewport' content='width=640'/>

这似乎在Chrome浏览器工作正常,但不能扩展到适合web视图。我做了一个简单的测试活动:

This seems to work fine in Chrome browser, but does not scale to fit in the WebView. I made a simplified test activity:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //--------------------------------------------------
        // Create a simple html page including viewport tag.
        //--------------------------------------------------

        String html = "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<meta name='viewport' content='width=640'/>" +
                "<title>Viewport Test</title>" +
                "</head>" +
                "<body style=\"margin: 0px;\">" +
                "<div style=\"width: 600px; height: 600px; border: 20px solid green; background-color: red;\"></div>" +
                "</body>" +
                "</html>";

        //-----------------------------------
        // Place html in WebView.
        //-----------------------------------

        WebView webView = new WebView(this);

        webView.loadData(html, "text/html", "utf-8");

        setContentView(webView);

        //-----------------------------------
        // Launch Chrome with the same html.
        //-----------------------------------

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));

        intent.setData(Uri.parse("data:text/html;charset=utf-8;base64," + Base64.encodeToString(html.getBytes(), Base64.NO_WRAP)));

        startActivity(intent);
    }
}

任何人都可以解释为什么,还是建议修复?

Can anyone explain why, or suggest a fix?

推荐答案

OK,你必须使用:

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

但同时,如果你设置的用户可扩展= 0这样它打破了:

But also, it breaks if you set user-scalable=0 like this:

    <meta name='viewport' content='width=640, user-scalable=0'/>

这篇关于在Android的web视图使用元宽视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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