主.java:
Main instanceMain = this;
String Dataz = FN.getHtmlData(instanceMain,"BMitra",20,(getResources().getString(R.string.matn2)));
btne.loadDataWithBaseURL("file:///android_asset/", Dataz, "text/html", "utf-8", null);
FN.java:
public static String getHtmlData(Context context,String fontname,Integer fontsize, String data){
String head = "<head><style type=\"text/css\">@font-face {font-family: 'MYFNT';src: url('"+fontname+".ttf');} body {text-align:justify;font-family: 'MYFNT';color: Black;}</style></head>";
String htmlData= "<html>"+head+"<body dir=\"rtl\">"+data+"</body></html>" ;
return htmlData;
}
字符串.xml:
<string name="matn2">
<![CDATA[
<html><body><b>سلام</b> non bold text . <b>english bold</b></body></html>
]]>
</string>
但粗体标签不起作用,输出为:سلام 英文粗体。非粗体文本
顺便说一句,我发现了一些报告问题
用另一种方式 :
主.java:
Main instanceMain = this;
String Dataz = FN.getHtmlData(instanceMain,getResources().getString(R.string.matn2)));
btne.loadDataWithBaseURL("file:///android_asset/", Dataz, "text/html", "utf-8", null);
FN.java:
public static String getHtmlData(Context paramContext, String paramString)
{
return "<!DOCTYPE HTML><html lang=\"fa-ir\" dir=\"rtl\"><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head>" + " <body>" + paramString + "</body></html>";
}
justified_textview.css(在 assest 文件夹中):
/*@font-face {
font-family:"MyFont";
src:url('file:///android_asset/BMITRA.TTF');
}
@font-face {
font-family:"MyFont";
src:url('file:///android_asset/BMITRABD.TTF');
font-weight: bold;
}*/
@font-face {
font-family: 'Conv_BMITRA';
src: url('file:///android_asset/BMITRA.ttf');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Conv_BMITRA';
src: url('file:///android_asset/BMITRABD.ttf');
font-weight: bold;
font-style: normal;
}
body {
font-family:'Conv_BMITRA';
text-align:justify;
line-height: 20pt;
color: Black;
}
和 matn2(strings.xml) :
<string name="matn">
<![CDATA[
م ولادت توی وب با سی اس اس
normal text
<b>ولایت بولد م bold text</b>
]]>
</string>
现在它适用于 android 2.3.3 或 2.3.5 但不适用于 android 4.0.x