我正在尝试为我的应用程序应用自定义字体样式。我知道:
Typeface tf = Typeface.createFromAsset(getAssets(),"font.ttf");
text.setTypeface(tf);
但我想为整个应用程序应用自定义字体样式,包括操作栏标签和所有文本视图。
是否有一种“晚餐”方式为整个应用程序应用自定义字体?
答案 0 :(得分:0)
您无法在一个点上为整个应用程序设置自定义字体,但您必须自己为每个视图设置字体。
请参阅此答案:Android: Want to set custom fonts for whole application not runtime
自定义ActionBar样式
答案 1 :(得分:0)
无法设置整个应用程序字体,但如果您正在寻找更通用的编程解决方案,我创建了一个静态类,可用于设置整个视图的字体(Activity UI)。请注意,我正在使用Mono(C#),但您可以使用Java轻松实现它。
您可以将此类传递给您要自定义的布局或特定视图。如果你想要超高效,可以使用Singleton模式实现它。
public static class AndroidTypefaceUtility
{
static AndroidTypefaceUtility()
{
}
//Refer to the code block beneath this one, to see how to create a typeface.
public static void SetTypefaceOfView(View view, Typeface customTypeface)
{
if (customTypeface != null && view != null)
{
try
{
if (view is TextView)
(view as TextView).Typeface = customTypeface;
else if (view is Button)
(view as Button).Typeface = customTypeface;
else if (view is EditText)
(view as EditText).Typeface = customTypeface;
else if (view is ViewGroup)
SetTypefaceOfViewGroup((view as ViewGroup), customTypeface);
else
Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View));
}
catch (Exception ex)
{
Console.Error.WriteLine("AndroidTypefaceUtility threw:\n{0}\n{1}", ex.GetType(), ex.StackTrace);
throw ex;
}
}
else
{
Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null");
}
}
public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface)
{
if (customTypeface != null && layout != null)
{
for (int i = 0; i < layout.ChildCount; i++)
{
SetTypefaceOfView(layout.GetChildAt(i), customTypeface);
}
}
else
{
Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null");
}
}
}
在您的活动中,您需要创建一个Typeface对象。我使用放在我的Resources / Assets /目录中的.ttf文件在OnCreate()中创建我的。确保该文件在其“属性”中标记为Android资产。
protected override void OnCreate(Bundle bundle)
{
...
LinearLayout rootLayout = (LinearLayout)FindViewById<LinearLayout>(Resource.Id.signInView_LinearLayout);
Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf");
AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface);
}