Shrink Webview Content width to fit the screen

2019-07-20 02:53发布

Im trying to shrink the width content of my webview down to fit with my screen so i dont have to scroll side way anymore...i tried so many code but it still doesnt anyone have any idea how??

heres my code so far.

    postView = (WebView) findViewById(R.id.postView);
    postView.setScrollBarStyle(WebView.OVER_SCROLL_NEVER);
    postView.setInitialScale((int) .8);
    WebSettings settings = postView.getSettings();
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

1条回答
【Aperson】
2楼-- · 2019-07-20 02:57

Well you can define width of webview with screenwidth itself and add it to its param section.

int screenWidth = getWindowManager()
                .getDefaultDisplay().getWidth();
postView.setLayoutParams(new TableRow.LayoutParams(
                    screenWidth / 2, LayoutParams.MATCH_PARENT, 1f));
/*I have assumed your webview's parent is tablerow. You must define layout params with respect to its parent. like if postview parentview is linearlayout then new LinearLayout.LayoutParams() must be used*/

This must solve your problem.

Hope, this helps.

Edit

Ok, now I get your problem. But I think in your case you should allow the user to get zoom in/out , so that user will get control for data he wants to view.

Add this statement

postView.getSettings().setBuiltInZoomControls(true);
查看更多
登录 后发表回答