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

Joh*_*n R 20 android webview text-size text-justify

WebView用来证明文本的合理性.我想知道-

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

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

文字大小为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);
       }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Kan*_*ngh 52

从布局设置文本大小 -

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

对于立即文本显示 -

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);
Run Code Online (Sandbox Code Playgroud)

在值文件夹中 -

<resources>

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

</resources>
Run Code Online (Sandbox Code Playgroud)

希望它有效.

  • 我测试你的代码.看来你不应该将字体大小从dimen传递给`setDefaultFontSize()`.例如,如果要以12 sp大小显示文本,则只需将12传递给该方法即可. (4认同)
  • 不推荐使用WebSettings类型的方法setRenderPriority(WebSettings.RenderPriority) (2认同)

Dig*_*tel 19

WebSettings webSettings = webView.getSettings();
Run Code Online (Sandbox Code Playgroud)

setTextSize或

webSettings.setTextSize(WebSettings.TextSize.SMALLEST);
Run Code Online (Sandbox Code Playgroud)

这个也有效:

webSettings.setDefaultFontSize(10);
Run Code Online (Sandbox Code Playgroud)

并立即显示文字:

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);
Run Code Online (Sandbox Code Playgroud)


nKn*_*nKn 7

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

WebSettings settings = yourWebView.getSettings();
Run Code Online (Sandbox Code Playgroud)

这样您就可以获得WebView正在尝试更改其文本大小的设置.

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

settings.setTextSize(TextSize.SMALLEST);
settings.setTextSize(TextSize.SMALLER);
settings.setTextSize(TextSize.NORMAL);
settings.setTextSize(TextSize.BIGGER);
settings.setTextSize(TextSize.BIGGEST);
Run Code Online (Sandbox Code Playgroud)

您可以根据所选当前设备的参数选择需要设置的那个.

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

settings.setDefaultFontSize(an_integer_that_goes_between_1_and_72);
Run Code Online (Sandbox Code Playgroud)

----编辑----

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

mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Run Code Online (Sandbox Code Playgroud)

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

android:hardwareAccelerated="true"
Run Code Online (Sandbox Code Playgroud)


Fan*_*mas 6

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="font_size">20</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

因此,在上面的代码中更改此行

webSettings.setDefaultFontSize(20);
Run Code Online (Sandbox Code Playgroud)

webSettings.setDefaultFontSize(fontSize);
Run Code Online (Sandbox Code Playgroud)

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

// Optimize loading times.
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);
Run Code Online (Sandbox Code Playgroud)

请注意,我加载了7至8行以上的文本:这是一个完整的HTML页面,其中包含JavaScript以展开/折叠部分。加载需要半秒钟。因此,我想一会就会载入7-8行文字。


Nir*_*tel 6

有三种方法可以FontSize开启WebView.

1)WebView.setDefaultFontSize(int)

webview.setDefaultFontSize(50);
Run Code Online (Sandbox Code Playgroud)

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

WebView.getSettings().setTextSize(TextSize.SMALLEST); 
Run Code Online (Sandbox Code Playgroud)

要么

WebView.getSettings().setTextSize(TextSize.SMALLER); 
Run Code Online (Sandbox Code Playgroud)

要么

WebView.getSettings().setTextSize(TextSize.NORMAL); 
Run Code Online (Sandbox Code Playgroud)

要么

WebView.getSettings().setTextSize(TextSize.BIGGER); 
Run Code Online (Sandbox Code Playgroud)

要么

WebView.getSettings().setTextSize(TextSize.BIGGEST);
Run Code Online (Sandbox Code Playgroud)

3)在html formate中添加font-size.

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>"; 
Run Code Online (Sandbox Code Playgroud)