說下自己在使用IconFont過程中遇到的一些問題
1、Font asset not found iconfont.ttf
這是因爲自己沒注意到Android studio的assets文件夾的位置跟eclipse的不太一樣
Android studio路徑爲:項目名/app/src/main/assets
Eclipse路徑爲:項目名/assets
2、在項目中大量使用時可以自定義個view,以減少代碼重複,如下
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by laisixiang on 2015/12/28.
*/
public class IconFontView extends TextView {
public IconFontView(Context context) {
super(context);
}
public IconFontView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public IconFontView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
Typeface iconfont = Typeface.createFromAsset(getContext().getAssets(), "iconfont.ttf");
this.setTypeface(iconfont);
}
}
在xml中引用記得全名
<com.app.views.IconFontView
android:id="@+id/iconfont
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/icon_to"/>
圖標獲取網站:http://www.iconfont.cn/collections
得到.ttf方法:http://blog.csdn.net/a345017062/article/details/46455745
摘要
http://blog.csdn.net/goodlixueyong/article/details/41448483
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);
}