public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.text);
AssetManager assets = getAssets();
Typeface fromAsset = Typeface.createFromAsset(assets, "fonts/fzlt.ttf");
textView.setTypeface(fromAsset);
}
}
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context,attrs);
// TODO Auto-generated constructor stub
}
public CustomTextView(Context context, AttributeSet attrs,int defStyle) {
super(context,attrs,defStyle);
// TODO Auto-generated constructor stub
}
public void setTypeface(Typeface tf, int style) {
super.setTypeface(AppContext.getInstance().getTypeface());
}
}
<style>
@font-face {
font-family: 'myface';
src: url('file:///android_asset/fonts/fzlt.ttf');
}
body {
margin: 0;
padding: 0;
font-family:'myface','方正兰亭纤黑简体';
}
.textbar{ box-sizing:border-box; width:100%; padding:5px;}
.textbar p{ font-size:16px; text-align:justify; color:#333;line-height:24px; margin:0 0 0 0;}
.textbar h1{ font-size:18px; margin:10px 0 10px 0;color:#000}
</style>
view.loadUrl("javascript:!function(){" + "s=document.createElement('style');s.innerHTML=" + "\"@font-face{font-family:myhyqh;src:url('**injection**/hyqh.ttf');}*{font-family:myhyqh !important;}\";"
+ "document.getElementsByTagName('head')[0].appendChild(s);" +
"document.getElementsByTagName('body')[0].style.fontFamily = \"myhyqh\";}()");
//由于网页上是没有权限访问本地的asset文件夹的,因此我们需要拦截请求来加载本地的文件,我这里替换了`file:
//android_assets/`为 `**injection**/`了,我们还需要重写`shouldInterceptRequest`
//在请求为我们这个字体文件的时候,加载本地文件:
@Override
public WebResourceResponse shouldInterceptRequest (WebView view, String url){
WebResourceResponse response = super.shouldInterceptRequest(view, url);
Log.i("load intercept request:" + url);
if (url != null && url.contains("**injection**/")) {
//String assertPath = url.replace("**injection**/", "");
String assertPath = url.substring(url.indexOf("**injection**/") + "**injection**/".length(), url.length());
try {
response = new WebResourceResponse("application/x-font-ttf", "UTF8", getAssets().open(assertPath));
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有