WebView字号设置
来源:互联网 发布:mac 移动硬盘绝对路径 编辑:程序博客网 时间:2024/04/30 10:28
下面示例是通过改变WebSettings的TextSize值来改变字号的,如果是html文本也可以用js来改变富文本的字体大小。
测试activity:
package com.home.webviewsize;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.Button;public class TestWebViewSizeActivity extends Activity implementsOnClickListener {private WebView webView;private Button enlargeBtn;private Button shrinkBtn;private WebSettings settings;private int fontSize = 1;// 用来控制字体大小@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);enlargeBtn = (Button) findViewById(R.id.main_btn_enlarge);shrinkBtn = (Button) findViewById(R.id.main_btn_shrink);enlargeBtn.setOnClickListener(this);shrinkBtn.setOnClickListener(this);webView = (WebView) findViewById(R.id.main_webview);settings = webView.getSettings();settings.setSupportZoom(true);getNetData();// 加载网络数据getInitialFontSize();// 得到文章字体的大小}/** * 得到文章字体的大小 */private void getInitialFontSize() {if (settings.getTextSize() == WebSettings.TextSize.SMALLEST) {fontSize = 1;} else if (settings.getTextSize() == WebSettings.TextSize.SMALLER) {fontSize = 2;} else if (settings.getTextSize() == WebSettings.TextSize.NORMAL) {fontSize = 3;} else if (settings.getTextSize() == WebSettings.TextSize.LARGER) {fontSize = 4;} else if (settings.getTextSize() == WebSettings.TextSize.LARGEST) {fontSize = 5;}}/** * 加载网络数据 */private void getNetData() {new Thread() {public void run() {webView.loadUrl("http://blog.csdn.net/u010142437/article/details/12679267");}}.start();}@Overridepublic void onClick(View v) {if (v == shrinkBtn) {fontSize--;refreshUI();// 根据字体大小刷新UI}if (v == enlargeBtn) {fontSize++;refreshUI();// 根据字体大小刷新UI}}/** * 根据字体大小刷新UI */private void refreshUI() {if (fontSize > 5) {fontSize = 5;}if (fontSize < 1) {fontSize = 1;}switch (fontSize) {case 1:settings.setTextSize(WebSettings.TextSize.SMALLEST);break;case 2:settings.setTextSize(WebSettings.TextSize.SMALLER);break;case 3:settings.setTextSize(WebSettings.TextSize.NORMAL);break;case 4:settings.setTextSize(WebSettings.TextSize.LARGER);break;case 5:settings.setTextSize(WebSettings.TextSize.LARGEST);break;}}}布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/main_layout_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" > <Button android:id="@+id/main_btn_enlarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="放大" /> <Button android:id="@+id/main_btn_shrink" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="缩小" /> </LinearLayout> <WebView android:id="@+id/main_webview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/main_layout_bottom" /></RelativeLayout>记得加上访问网络的权限:
<uses-permission android:name="android.permission.INTERNET" />
- WebView字号设置
- 针式打印机设置字号大小
- AS设置字体字号
- RichEdit根据字号设置文字大小
- idea背景/字号/tomcat设置
- 字号
- WebView设置
- WebView设置
- WebView设置
- webview设置
- Webview设置
- 禁止APP内Webview页面跟随系统缩放字号
- Qt 设置字体 字号等格式属性
- CategoryAxis的标签设置字体字号等
- cocos2dx EditBox 设置字号大小的问题
- 设置字体、字号等格式属性
- iOS7 textFeild的palcehole字号设置
- Unity3D 设置Label TextField字体字号大小
- sql server2005配置管理器问题
- vs2008异常
- hdu3681 PrisonBreak
- vs中的类和函数
- putty连接报network error :conncetionrefused
- WebView字号设置
- 使用jsmooth将jar文件转换成可在无java环境中运行的exe文件
- LeetCode题解:Pow(x,n)
- android开发报java.lang.reflect.InvocationTargetException
- onActivityResult的用法
- 对话框控件访问七种方式转自孙鑫
- NSPredicate使用
- c# 里面写了一个 FIFO 类, 传递数据非常实用
- Mahout之minhash