如何防止webview在方向更改时更改文字大小?

时间:2016-09-14 11:57:48

标签: java android webview screen-orientation text-size

我正在动态地将webview添加到线性布局中。我的问题是当方向改变时文本大小也会改变。例如,当应用程序处于横向模式时,文本大小会说大,而在纵向模式下它是中等。我想保持文字大小适应方向变化。 这是我试图实现的代码: -

private void handle_webcontent(String contentType, final String content) {
    String html_open = "<html><head><style>@font-face {font-family: 'verdana';src: url('file://"+ this.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style><title></title></head><body>";
    String html_close = "</body></html>";
    final WebView webView1;
    webView1 = new WebView(this);
    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        layoutParam.setMargins(10,0,10,10);
        webView1.setLayoutParams(new LinearLayout.LayoutParams(layoutParam));

    }
    web_linearLayout.addView(webView1);
    switch (contentType) {
        case "text":
            if (content.equals("NA")) {
                Log.d("CONTENT", "NOTHING TO DISPLAY");

            } else {
                Document doc = Jsoup.parse(html_open);
                doc.append(content);
                doc.append(html_close);
                webView1.getSettings().setJavaScriptEnabled(true);
                webView1.setInitialScale(getScale());
                webView1.getSettings().setAllowFileAccess(true);
                webView1.getSettings().setLoadsImagesAutomatically(true);
                webView1.setWebViewClient(
                        new WebViewClient() {

                            @Override
                            public void onPageFinished(WebView view, String url) {
                                String javascriptCode = "javascript:";
                                javascriptCode+="var elements=document.querySelectorAll(\"code\");";
                                javascriptCode+="var parent;";
                                javascriptCode+="var container;";
                                javascriptCode+="for(i in elements){";
                                javascriptCode+="container = document.createElement('div');";
                                javascriptCode+="parent = elements[i].parentElement;";
                                javascriptCode+="parent.replaceChild(container, elements[i]);";
                                javascriptCode+="container.appendChild(elements[i]);";
                                javascriptCode+="container.setAttribute(\"style\",\" white-space: nowrap; background-color: #EEEEEE;  padding-top: 4px;  padding-right: 4px;  padding-bottom: 4px;  padding-left: 4px;\");}";
                                webView1.loadUrl(javascriptCode);
                            }
                        }
                );
                webView1.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "UTF-8", "");
                Log.d("CODE CONTENT",String.valueOf(doc));


            }

            break;
        case "image":
            if (content.equals("NA")) {
                Log.d("CONTENT", "NOTHING TO DISPLAY");
            } else {
                String image = image_url + content;
                downloadImages(image);
            }
            break;
        case "button":
            if (content.equals("NA")) {
                Log.d("CONTENT", "NOTHING TO DISPLAY");

            } else {
                Button button = (Button) getLayoutInflater().inflate(R.layout.button_template, null);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
                button.setLayoutParams(layoutParams);
                web_linearLayout.addView(button);

                final WebView webView;
                webView = new WebView(this);
                webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                web_linearLayout.addView(webView);


                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String html_open = "<html><head><style>@font-face {font-family: 'verdana';src: url('file://"+ DisplayContent.this.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style><title></title></head><body>";
                        String html_close = "</body></html>";
                        Document doc = Jsoup.parse(html_open);
                        doc.append(content);
                        doc.append(html_close);
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.getSettings().setAllowFileAccess(true);
                        webView.getSettings().setLoadsImagesAutomatically(true);
                        webView.loadDataWithBaseURL("", String.valueOf(doc), "text/html", "utf-8", "");

                    }
                });
            }

            break;
        default:
    }

}

感谢任何帮助或建议。谢谢。

1 个答案:

答案 0 :(得分:1)

在你的活动中提到这个

android:configChanges="keyboardHidden|orientation"

android:configChanges="orientation|screenSize"