如何在android中的WebView中设置文本大小 [英] How to set text size in WebView in android

查看:39
本文介绍了如何在android中的WebView中设置文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WebView 来对齐文本.我想知道-

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

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

  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?

文本大小为 7-8 行.

代码-

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>

推荐答案

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

For setting text size from layout-

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

对于立即文本显示-

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

在值文件夹中-

<resources>

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

</resources>

希望它有效.

这篇关于如何在android中的WebView中设置文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆