如何在android中的WebView中设置文本大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21694306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:56:28  来源:igfitidea点击:

How to set text size in WebView in android

androidwebviewtext-sizetext-justify

提问by John R

I am using WebViewto justify text. I want to know-

WebView用来证明文本。我想知道-

  1. Is it possible to set the text size in layoutfile? As I want different text size for different screen sizes.

  2. And also one problem is first background appears then after 2 second text display. Is it possible to display text immediately?

  1. 是否可以在layout文件中设置文本大小?因为我想要不同的屏幕尺寸有不同的文字大小。

  2. 还有一个问题是首先出现背景,然后在 2 秒文本显示后出现。是否可以立即显示文本?

Size of text is 7-8 lines.

文字大小为 7-8 行

Code-

代码-

public class Main extends Activity {

       WebView mWebView;

       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);        
          mWebView = (WebView) findViewById(R.id.webview);
          String text = "<html><body>"+"<p align=\"justify\">"+getString(R.string.itext)+"</p>"+"</body></html>"; 
      mWebView .loadData(text, "text/html", "utf-8");
      mWebView .setBackgroundColor(Color.TRANSPARENT);
       }
}

Xml-

xml-

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical" 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" 
       android:background="@drawable/light">

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent"/>
</LinearLayout>

回答by Kanwaljit Singh

For setting text size from layout-

用于从布局设置文本大小 -

final WebSettings webSettings = web.getSettings();
Resources res = getResources();
fontSize = res.getDimension(R.dimen.txtSize);
webSettings.setDefaultFontSize((int)fontSize);

For Immediate text display-

对于立即文本显示-

webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);

In values folder-

在值文件夹中-

<resources>

    <dimen name="txtSize">26sp</dimen>

</resources>

Hope it works.

希望它有效。

回答by Digvesh Patel

WebSettings webSettings = webView.getSettings();

either setTextSize or

要么 setTextSize 要么

webSettings.setTextSize(WebSettings.TextSize.SMALLEST);

This one works too:

这个也有效:

webSettings.setDefaultFontSize(10);

And Display text immediately:

并立即显示文本:

webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.setBlockNetworkImage(true);
webview.setLoadsImagesAutomatically(true);
webwebviewSettings.setGeolocationEnabled(false);
webview.setNeedInitialFocus(false);
webview.setSaveFormData(false);

回答by nKn

I'm not sure if this is what you're looking for, but as long as you don't want to set the text size based on a calculation with the text width/height, this should be what you're looking for:

我不确定这是否是您要查找的内容,但只要您不想根据文本宽度/高度的计算来设置文本大小,这应该是您要查找的内容:

WebSettings settings = yourWebView.getSettings();

This way you're getting the settings of the WebViewwhose text size you're trying to change.

通过这种方式,您将获得WebView要更改其文本大小的设置。

There're some predefined values you can set as size using the TextSizeclass, for instance:

您可以使用TextSize该类将一些预定义值设置为大小,例如:

settings.setTextSize(TextSize.SMALLEST);
settings.setTextSize(TextSize.SMALLER);
settings.setTextSize(TextSize.NORMAL);
settings.setTextSize(TextSize.BIGGER);
settings.setTextSize(TextSize.BIGGEST);

You could simply choose which one you need to set depending on the parameters of the current device that you choose.

您可以根据您选择的当前设备的参数简单地选择需要设置的那个。

If you, on the contrary, want to make some kind of screen width/height calculation and set the result as a particular value, you may even use this:

相反,如果您想要进行某种屏幕宽度/高度计算并将结果设置为特定值,您甚至可以使用:

settings.setDefaultFontSize(an_integer_that_goes_between_1_and_72);

---- EDIT ----

- - 编辑 - -

To enhace your WebViewrendering, try the following:

要增强WebView渲染效果,请尝试以下操作:

mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

You could also try adding this to your Manifestfile:

您也可以尝试将其添加到您的Manifest文件中:

android:hardwareAccelerated="true"

回答by Phant?maxx

1 - I'm using this code that gives me a good result on all my devices.

1 - 我正在使用这段代码,它在我的所有设备上都得到了很好的结果。

If web is tyour WebView,

如果 web 是你的 WebView,

web = (WebView) v.findViewById(R.id.htmlDisplay);
// Impostazioni della WebView.
final WebSettings webSettings = web.getSettings();
// Set the font size (in sp).
webSettings.setDefaultFontSize(20);

OK, this is hardcoded

好的,这是硬编码的

Now, a more dynamic solution (as per your question):

现在,一个更动态的解决方案(根据您的问题):

If you put your desired size in an integers file (several, one for each screen you are supporting), you could do so to retrieve its value:

如果你把你想要的大小放在一个整数文件中(几个,你支持的每个屏幕一个),你可以这样做来检索它的值:

Resources res = getResources();
float fontSize = res.getInteger(R.integer.font_size);
webSettings.setDefaultFontSize(fontSize);

Assuming that your res/values/integers.xml file(s) is similar to this one:

假设您的 res/values/integers.xml 文件与此类似:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="font_size">20</integer>
</resources>

So change this line in the above code

所以在上面的代码中改变这一行

webSettings.setDefaultFontSize(20);

to

webSettings.setDefaultFontSize(fontSize);

2 - To load things faster, I use the following code

2 - 为了更快地加载东西,我使用以下代码

// Optimize loading times.
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);

Note that I load more than 7-8 lines of text: it's a full HTML page with JavaScript to expand / collapse sections. And it takes, say, half a second to load. So, I guess, 7-8 lines of text will be loaded in a flash.

请注意,我加载了超过 7-8 行的文本:这是一个完整的 HTML 页面,使用 JavaScript 来展开/折叠部分。比如说,加载需要半秒钟。所以,我想,7-8 行文本将在瞬间加载。

回答by Niranj Patel

There is three way to set FontSizeon WebView.

还有就是要设置三种方式FontSizeWebView

1) WebView.setDefaultFontSize(int)

1) WebView.setDefaultFontSize(int)

webview.setDefaultFontSize(50);

2) WebView.getSettings().setTextSize(WebSettings.TextSize.SMALLEST)

2) WebView.getSettings().setTextSize(WebSettings.TextSize.SMALLEST)

WebView.getSettings().setTextSize(TextSize.SMALLEST); 

or

或者

WebView.getSettings().setTextSize(TextSize.SMALLER); 

or

或者

WebView.getSettings().setTextSize(TextSize.NORMAL); 

or

或者

WebView.getSettings().setTextSize(TextSize.BIGGER); 

or

或者

WebView.getSettings().setTextSize(TextSize.BIGGEST);

3) Add font-size in html formate.

3)在html格式中添加字体大小。

String text = "<html><body style=\"color:black;font-family:Helvetica;font-size:12px;\"'background-color:transparent' >"+"<p align=\"justify\">"+getString(R.string.itext)+"</p>"+"</body></html>"; 

回答by Luciano Rodríguez

You can always use CSS to change the font size:

您始终可以使用 CSS 来更改字体大小:

String text = "<html><style type=\"text/css\">p{text-align:justify;font-size:80%;}</style></head><body>"+"<p>"+getString(R.string.text1)+"</p>"+"</body></html>";

mWebView.loadData(text, "text/html", "utf-8");

To display fater, as Digvesh Patel said before:

为了展示父亲,正如 Digvesh Patel 之前所说:

webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

回答by user8938

You can use Html.fromHtml()and write your text in HTMLformat.

您可以Html.fromHtml()HTML格式使用和编写文本。

回答by Al Walid Ashik

In kotlin just add this line,

在 kotlin 中只需添加这一行,

webview.settings.defaultFontSize = yourSizeInInteger

回答by sana ebadi

webView.getSettings().setDefaultFontSize(5); //5 or any dmen you want

回答by maulik panchal

webSettings.setTextSize is depricated now onwards.

webSettings.setTextSize 现在已弃用。

You can Use setTextZoomOR getTextZoomfor changing the size of webview font.

您可以使用setTextZoomgetTextZoom来更改 webview 字体的大小。

For Regular textsize you can give the integer value as 100. And If you want to increase size of font then you can use code as below :

对于常规文本大小,您可以将整数值设为 100。如果您想增加字体大小,则可以使用如下代码:

webView.settings.textZoom = 125