是否可以为活动中的所有TextViews设置字体?我可以使用以下命令设置单个textView的字体:
TextView tv=(TextView)findViewById(R.id.textView1);
Typeface face=Typeface.createFromAsset(getAssets(), "font.ttf");
tv.setTypeface(face);
但是我想一次更改所有的textViews,而不是为每个textView手动设置,任何信息都将不胜感激!
发布于 2012-05-26 21:47:18
Solution1::只需通过将父视图作为参数传递来调用这些方法。
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView ) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}
Solution2::您可以使用自定义字体对textview类进行子类化,并使用它来代替TextView。
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}
}
发布于 2016-02-12 20:29:04
我个人收藏的那一本:
private void setFontForContainer(ViewGroup contentLayout) {
for (int i=0; i < contentLayout.getChildCount(); i++) {
View view = contentLayout.getChildAt(i);
if (view instanceof TextView)
((TextView)view).setTypeface(yourFont);
else if (view instanceof ViewGroup)
setFontForContainer((ViewGroup) view);
}
}
发布于 2013-07-31 06:24:20
如果您正在寻找更通用的编程解决方案,我创建了一个静态类,可用于设置整个视图(Activity UI)的字体。请注意,我使用的是Mono (C#),但您可以使用Java轻松实现它。
您可以向此类传递要自定义的布局或特定视图。如果你想变得超级高效,你可以使用单例模式来实现它。
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);
}
https://stackoverflow.com/questions/10766716
复制相似问题