欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年10月10日 14:15:18   投稿:lqh  
這篇文章主要介紹了Android APP使用自定義字體實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下

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í)測(cè)這幾種字體僅英文有效),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','方正蘭亭纖黑簡(jiǎn)體';
}
.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í)候,加載本地文件:
@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;
}



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

相關(guān)文章

最新評(píng)論