gpt4 book ai didi

java - 如何防止 webview 在方向改变时改变文本大小?

转载 作者:行者123 更新时间:2023-12-01 09:30:15 25 4
gpt4 key购买 nike

我正在将 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:
}

}

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

最佳答案

在您的 Activity 中提及这一点

android:configChanges="keyboardHidden|orientation"

android:configChanges="orientation|screenSize"

关于java - 如何防止 webview 在方向改变时改变文本大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489883/

25 4 0
文章推荐: python - 如何在 Jupyter 笔记本的 LaTeX 中自定义 numpy 数组列括号的显示格式?
文章推荐: ruby-on-rails - 有人可以在 Rails 中用 Ruby 定义一个引擎吗?
文章推荐: java - 无法使用 Selenium WebDriver 单击
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com