【发布时间】:2014-08-08 00:29:40
【问题描述】:
假设我想创建一个类来保存我从资产文件夹中获取的所有字体,这可能吗?
如果不导入android.app.activity,我就无法访问getAssets()。
实用类
import android.graphics.Typeface;
public class TypeFontAssets //possibly missing an extends?
{
public static Typeface cs = Typeface.createFromAssets(getAssets(), "fonts/ComicSans.ttf");
public static Typeface bh = Typeface.createFromAssets(getAssets(), "fonts/BradleyHand.ttf");
public static Typeface co = Typeface.createFromAssets(getAssets(), "fonts/Courier.ttf");
}
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this class for ALL the fonts
TypeFaceAssets tfa = new TypeFaceAssets();
TextView userName = (TextView)findViewById(R.id.userNameView);
//trying to get comic sans from the tfa object
userName.setTypeface(tfa.cs);
}
}
【问题讨论】:
标签: android android-resources android-assets android-typeface