如何让android WebView自适应?

时间:2015-02-16 22:33:36

标签: android html5 web webview

我有JS的html5。它看起来很不错,但对于(Nexus 5)屏幕来说太大了,WebView变得可滚动。 我正在寻找解决方案以使这种观点具有适应性。

WebView mWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamelayout);

        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        mWebView.setBackgroundColor(Color.parseColor("#001A0F"));
        mWebView.zoomOut();
        mWebView.loadUrl("file:///android_asset/anim1/Untitled-1.html");

    }

1 个答案:

答案 0 :(得分:0)

就像评论中提到的那样,您需要更改html。

使用this question中的答案来阅读所需的颜色。对我来说,我需要阅读colorSurface,即:

        int bgColorResId = R.attr.colorSurface;
        TypedValue typedValue = new TypedValue();
        getContext().getTheme().resolveAttribute(bgColorResId, typedValue, true);
        int bgColor = typedValue.data

然后我需要转换int to a hexa string

String colorStr = String.format("#%06X", 0xFFFFFF & bgColor)

最后,您需要将其添加到html字符串中,例如

"<body bgcolor=\"" + colorStr + "\">"

您也可以为文本颜色设置颜色。在此处输入文字颜色

<style type="text/css">
body { color: #TEXTCOLOR };
</style>