国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android APP使用自定義字體實(shí)現(xiàn)方法

2019-12-12 05:06:28
字體:
供稿:網(wǎng)友

android系統(tǒng)內(nèi)置字體

android 系統(tǒng)本身內(nèi)置了一些字體,可以在程序中使用,并且支持在xml配置textView的時(shí)候進(jìn)行修改字體的樣式。支持字段為android:textStyle ,android:typeface, android:fontFamily,系統(tǒng)內(nèi)置了normal|bold|italic三種style, 內(nèi)置了normal,sans,serif,monospace,幾種字體(實(shí)測這幾種字體僅英文有效),typace和fontFamily功能一樣。

使用自定義的字體

以上的方式可以改變字體的樣式,還不是真正的自定義。android系統(tǒng)支持TypeFace,即ttf的字體文件。我們可以在程序中放入ttf字體文件,在程序中使用Typeface設(shè)置字體。

第一步,在assets目錄下新建fonts目錄,把ttf字體文件放到這。

第二步,程序中調(diào)用:

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);  }}

注意ttf文件命名不能使用中文,否則可能無法加載。

對(duì)于需要使用比較多的地方,可以寫一個(gè)TextView的子類來統(tǒng)一處理。

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());   } }

//初始化自定義字體
typeface = Typeface.createFromAsset(getAssets(), "fonts/fzlt.ttf");

法還是有點(diǎn)缺點(diǎn)的:只能替換一類控件的字體,如果需要替換Button或EditText控件的字體,需要以相同的方式自定義這些控件,這樣工作量大,如何高效替換整個(gè)app中的字體,見下方參考資料。

在webview中使用自定義的字體

對(duì)于本地的網(wǎng)頁,在asset目錄放字體文件,并在css中添加以下內(nèi)容,自定義一個(gè)字體face,并且在需要的地方使用這個(gè)字體face即可。

<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>

對(duì)于在線的網(wǎng)頁,則需要把字體文件放到服務(wù)器,使用同樣的方式定義字體face,應(yīng)用到每個(gè)地方。
為了減少網(wǎng)頁或者說服務(wù)器端的工作,可以使用本地注入的方式注入font-face的css,并對(duì)整個(gè)網(wǎng)頁進(jìn)行樣式替換。給webview自定義webViewClient,重寫onPageFinish,在其中添加如下內(nèi)容:

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/";}()"); //由于網(wǎng)頁上是沒有權(quán)限訪問本地的asset文件夾的,因此我們需要攔截請(qǐng)求來加載本地的文件,我這里替換了`file://android_assets/`為 `**injection**/`了,我們還需要重寫`shouldInterceptRequest`//在請(qǐng)求為我們這個(gè)字體文件的時(shí)候,加載本地文件:@Overridepublic 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;}

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂宁市| 湘潭县| 桦川县| 金门县| 诏安县| 江华| 临桂县| 旬阳县| 策勒县| 平安县| 湾仔区| 贵定县| 开封县| 葫芦岛市| 吉安县| 天峨县| 奉新县| 徐闻县| 迁安市| 崇礼县| 洛阳市| 古蔺县| 静宁县| 同仁县| 和静县| 衡南县| 鹤岗市| 常山县| 伽师县| 汝城县| 贵溪市| 香港 | 银川市| 鄢陵县| 于都县| 米泉市| 朔州市| 灯塔市| 昭觉县| 乐山市| 济源市|