原文:http://blog.isming.me/2015/07/07/android-custom-font/

最近的一个项目中有个需求:app与webview需要统一使用自定义字体。

一:修改App的默认字体

1.把myfont.ttf放在assets/fonts 目录下

2.配置 Calligraphy

dependencies { compile 'uk.co.chrisjenx:calligraphy:1.2.0' githut地址:https://github.com/chrisjenx/Calligraphy

2.在 BaseApplication extends Application 的onCreate 方法中

 CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/myfont.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
3.在BaseActivity 中重写attachBaseContext方法
 protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
二:修改webview里网页的字体:

1. 对于本地的网页,在asset目录放字体文件,并在css中添加以下内容,自定义一个字体face,并且在需要的地方使用这个字体face即可。

@font-face {
	  font-family: 'MyCustomFont';
	  src: url('file:///android_asset/fonts/myfont.ttf'); 
	body{
	  font-family:"MyCustomFont";

2.对于在线的网页,则需要把字体文件放到服务器,使用同样的方式定义字体face,应用到每个地方。

为了减少网页或者说服务器端的工作,可以使用本地注入的方式注入font-face的css,并对整个网页进行样式替换。

给webview自定义webViewClient,重写onPageFinish,在其中添加如下内容:

public void onPageFinished(WebView view, String url) {
				injectCSS();
				view.loadUrl("javascript:!function(){" +
				        "s=document.createElement('style');s.innerHTML="
				        + "\"@font-face{font-family:myhyqh;src:url('****/fonts/myfont.ttf');}*{font-family:myhyqh !important;}\";"
				        + "document.getElementsByTagName('head')[0].appendChild(s);" +
				        "document.getElementsByTagName('body')[0].style.fontFamily = \"myhyqh\";}()");
				super.onPageFinished(view, url);
****/fonts/myfont.ttf  //这里的* 号是为了让它走:shouldInterceptRequest
重写vwebview的shouldInterceptRequest方法:
public WebResourceResponse shouldInterceptRequest(WebView view,
					String url) {
				WebResourceResponse response = super.shouldInterceptRequest(view, url);
				Log.i("webview","load intercept request:" + url);
				 if (url != null && url.contains("myfont.ttf")) {
				        String assertPath ="fonts/myfont.ttf";
				        try {
				            response = new WebResourceResponse("application/x-font-ttf","UTF8", getAssets().open(assertPath));
				        } catch (IOException e) {
				            e.printStackTrace();
				return response;

这样网页被新字体重新渲染就达到目的了 原文:http://blog.isming.me/2015/07/07/android-custom-font/最近的一个项目中有个需求:app与webview需要统一使用自定义字体。一:修改App的默认字体1.把myfont.ttf放在assets/fonts 目录下2.配置Calligraphydependencies {    compile 'uk.co.chrisjenx:calligr...
React-Native  Android 与 IOS 共用代码 React-Native 开发的App, 所有组件iOS & Android 共用, 共享一份代码 包括一些自定义的组件, 如NavigationBar, TabBar, SegmentedControl, 使用字体图标, 具有一定的参考意义 主要专注于布局, 共享组件/代码, 以及一些React自带的组件, 如: ScrollView, TouchableOpacity, View, Text, ListView, Image, WebView 和 Api的使用, 这并不是一个完整版, 只是业余时间, 对web版的一个nati
从assets中获取文件      file:///android_asset/page1.jpg 从drawable中获取图片  file:///android_res/drawable/page1.jpg 从font中获取字体           file:///android_res/font/font.otf 其他类型举一反三吧 以下内容没有验证: Using the re...
今天在做项目时,碰到了要设置webview字体大小,以前用的时候也没设置过,因此也不会,于是到处找关于“如何设置webview字体大小”的资料,好多大神给出的答案都是各种判断,稍显麻烦,我是最怕麻烦的人了,于是继续查资料,最后总结如下,简单几行代码就搞定 private WebSettings settings; mJkjyMbzsDetailWebView = (WebV
android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> 2. 在Activity中获取WebView组件的引用: WebView webView = (WebView) findViewById(R.id.webview); 3. 设置WebView的属性,如启用JavaScript、缩放等: webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBuiltInZoomControls(true); 4. 加载网页: webView.loadUrl("http://www.example.com"); 5. 处理WebView的各种事件,如页面加载完成、页面加载失败等: webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { // 页面加载完成 @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // 页面加载失败 以上就是使用Android Studio中WebView的基本步骤。