1:android 默认字体
通过布局设置:
android:textStyle="bold"
android:typeface="sans"
在Android SDK中使用Typeface类来定义字体,可以通过常用字体类型名称进行设置,如设置默认黑体:
TextView textView =(TextView)findViewById(R.id.custom);
textView.setTypeface(Typeface.DEFAULT_BOLD);
常用的字体类型名称还有:
* Typeface.DEFAULT //常规字体类型
* Typeface.DEFAULT_BOLD //黑体字体类型
* Typeface.MONOSPACE //等宽字体类型
* Typeface.SANS_SERIF //sans serif字体类型
* Typeface.SERIF //serif字体类型
除了字体类型设置之外,还可以为字体类型设置字体风格,如设置粗体:
Paint mp = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTypeface( font );
常用的字体风格名称还有:
* Typeface.BOLD //粗体
* Typeface.BOLD_ITALIC //粗斜体
* Typeface.ITALIC //斜体
* Typeface.NORMAL //常规
2:paint 设置字体
Paint mp = new Paint();
mp.setFakeBoldText(true); //true为粗体,false为非粗体
mp.setTextSkewX(-0.5f); //float类型参数,负数表示右斜,整数左斜
mp.setUnderlineText(true); //true为下划线,false为非下划线
mp.setStrikeThruText(true); //true为删除线,false为非删除线
Paint常用的方法还有:
mp.setTextSize(); //设置字体大小,int型,如12
mp.setStrokeWidth(w); //设置线宽,float型,如2.5f,默认绘文本无需设置(默认值好像为0),但假如设置了,再绘制文本的时候一定要恢复到0
3:设置自定义字体
把字体文件放在assets/fonts目录。fonts如果没有。新建该目录。字体文件可以是xx.oft, xx.ttf, xx.ttc等类型文件
TextView textView =(TextView)findViewById(R.id.custom);
//将字体文件保存在assets/fonts/目录下,创建Typeface对象
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonnts/fangsongti.ttf");
//使用字体成仿宋体
textView.setTypeface(typeFace);
注意,当字体文件比较小的时候。加载没有什么问题。当字体文件比较大的时候,会比较慢,要放在Application中
public static Typeface typeFace1
@Override
public void onCreate() {
super.onCreate();
typeFaceZh= Typeface.createFromAsset(getAssets(), "fonts/simsun.ttc");
}
4:为了使用起来方便,还可以将设置字体的操作封装成一个工具类
public class TypefaceUtils {
public static void setTypeface( TextView tv){
tv.setTypeface(MyAppliction.typeFace1);
}
public static void setTypeface( Button tv){
tv.setTypeface(MyAppliction.typeFaceZh);
}
public static void setTypeface( EditText tv){
tv.setTypeface(MyAppliction.typeFace1);
}
public static void setTypeface(Paint tv){
tv.setTypeface(MyAppliction.typeFace1);
}
* 设置字体加粗 public static void setBold(TextView tv, boolean isBold) { TextPaint tp = tv.getPaint(); tp.setFakeBoldText(isBold); }
}
5:webview 设置自定义字体
将在Assets目录下新建两个文件myfont.css和body.css
myfont.css
@font-face { font-family: '宋体'; src:url('fonts/xxxx1.ttf') format('truetype'); font-weight: normal; font-style: normal;}
这里需要注意的问题:如果有两个或者两个以上的字体文件,在上面的css中可以依次写入即可,一个@font-face{}标签里边写一个字体。另外font-family后边的名字可以相同,也可以不同!
boby.css
body { font-family:'宋体';}
我这里是让网页中<body />里边使用这些字体文件,当然你也可以做你自己的处理!另外需要注意的问题,在myfont.css中如果`font-family写了相同的名字,则写一个名字即可,但是如果写了不同的名字,则要写上全部的名字,中间用逗号隔开!
③接下来,就要对网页进行处理,加入我们的css!
注意。如果是服务器文本编辑器传过来的,加上html标签头部htmlConntent = "<html><head>" + "</head><body>" + htmlConntent + "</body></html>";
String assetsFontCSS = "<link href=\"file:///android_asset/myfont.css\" rel=\"stylesheet\" type=\"text/css\"/>";String assetsbodyCSS = "<link href=\"file:///android_asset/body.css\" rel=\"stylesheet\" type=\"text/css\"/>";
String htmlAdded = htmlConntent.replace("</head>", assetsFontCSS + "\n" + assetsbodyCSS + "\n" + "</head>");
print(htmlConntent + "" + htmlAdded);
webView.loadDataWithBaseURL(htmlConntent, htmlAdded, "text/html", "utf-8", null);
将两个css,引入到HTML源码的<head />标签中!
④最后调用loadDataWithBaseUrl()方法即可!
webView.loadDataWithBaseURL(htmlConntent, htmlAdded , "text/html","utf-8", null);