Android选择自定义字体

时间:2012-10-03 13:12:06

标签: android fonts

如何让用户选择字体?在“首选项”中,我想要更改字体和字体列表。

我已经用这种方式设置了字体:

mTimerLabel = (TextView)findViewById(R.id.label); 
Typeface typeface = Typeface.createFromAsset(getAssets(), "Fonts/digital-7i.ttf");
mTimerLabel.setTypeface(typeface);

3 个答案:

答案 0 :(得分:2)

您已经在textView中描述了更改字体的正确模型。我无法理解你遇到的问题。调用textView的首选项,选择字体(或其id)并进行设置。

答案 1 :(得分:2)

您必须将ttf存储在assets文件夹中。此外,AFAIK你不能用大写字母命名任何文件夹 - 就像你在Fonts中所做的那样。其余的方法很好。您可能希望使用Logcat检查您在首选项中存储的值。

答案 2 :(得分:1)

在首选项中存储字体名称,即digital-7i.ttf,当用户选择任何字体时,您可以从首选项中替换名称并使用您的代码。

SharedPreferences Settings = getSharedPreferences(
            "<PREF_NAME>", MODE_PRIVATE);
    fontName = Settings.getString("<KEY>", "digital-7i.ttf");


mTimerLabel = (TextView)findViewById(R.id.label); 
Typeface typeface = Typeface.createFromAsset(getAssets(), "Fonts/"+fontName);
mTimerLabel.setTypeface(typeface);