-->

How to set text size in WebView in android

2019-01-16 20:48发布

问题:

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

  1. Is it possible to set the text size in layout file? 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?

Size of text is 7-8 lines.

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-

  <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>

回答1:

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.



回答2:

WebSettings webSettings = webView.getSettings();

either setTextSize or

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);


回答3:

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 WebView whose text size you're trying to change.

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

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 WebView rendering, try the following:

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

You could also try adding this to your Manifest file:

android:hardwareAccelerated="true"


回答4:

There is three way to set FontSize on WebView.

1) WebView.setDefaultFontSize(int)

webview.setDefaultFontSize(50);

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.

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>"; 


回答5:

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

If web is tyour 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:

<?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

// 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.



回答6:

You can always use CSS to change the font size:

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:

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


回答7:

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