天天看點

在Android開發中使用icon font的代碼和方法 在Android開發中使用icon font的代碼和方法

在Android開發中使用icon font的代碼和方法

IconFont字型不僅僅流行語Web開發,在移動開發中也漸漸的使用的範圍更廣泛。這裡介紹在Android開發中使用icon font的代碼和方法。 

應用步驟:

1、第一步:複制字型檔案到項目 assets 目錄;

2、第二步:打開 iconfont 目錄中的 demo.html,找到圖示相對應的 HTML 實體字元碼;

3、第三步:打開 res/values/strings.xml,添加 string 值;

<string name="icons">㘅 㖭 㖮 㖯</string>
           

4、第四步:打開 activity_main.xml,添加 string 值到 TextView:

<TextView
    android:id="@+id/like"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/icons" />
           

5、第五步:為 TextView 指定文字:

import android.graphics.Typeface;
 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");
    TextView textview = (TextView)findViewById(R.id.like);
    textview.setTypeface(iconfont);
}