繁体   English   中英

如何防止Webview在更改方向时更改文本大小?

[英]How to prevent webview from changing text size on orientation change?

我正在动态添加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:
    }

}

任何帮助或建议,谢谢。

在您的活动中提及此

android:configChanges="keyboardHidden|orientation"

要么

android:configChanges="orientation|screenSize"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM