2012-01-21 52 views
17

从一个应用程序,我发展的需要,我已经收到了,有许多文件,如FONTNAME正规,FONTNAME粗体FONTNAME,它特定字体。我需要在应用程序的所有文字视图中使用它。首先,我认为这是一件容易的事。查看SO,发现了一个非常漂亮的主题:here自定义字体和自定义的TextView在Android

所以首先我不喜欢:

public static 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(FONT_REGULAR); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     // ignore 
    } 
} 

而且叫我活动的onCreate期间此方法。我的应用程序中的每个textView都显示该字体和男孩,我很高兴能够轻松离开。直到我进入一个需要一些文字浏览的屏幕Bold as Styleandroid:textStyle="bold")。然后我意识到这个解决方案不能为我提供从资产中加载Font-Bold .ttf的可能性。

不是进一步望去,只见一个漂亮的自定义TextView的实施,在相同的SO问题:

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(); 
    } 

    public void init() { 

     Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf"); 
     setTypeface(tf ,1); 

    } 
    } 

这看起来甚至更好。我的问题是:如何在init()上检测我的控件是否将样式设置为粗体,以便我可以分配请求的类型?

谢谢你的时间。

LE。

public class MyTextView extends TextView { 

    Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR); 
    Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_BOLD); 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyTextView(Context context) { 
     super(context); 
    } 

    public void setTypeface(Typeface tf, int style) { 
     if (style == Typeface.BOLD) { 
      super.setTypeface(boldTypeface/*, -1*/); 
     } else { 
      super.setTypeface(normalTypeface/*, -1*/); 
     } 
    } 
} 

那么如果我调试,应用程序进去setTypeFace,它似乎适用大胆的一个,但在我的布局,我看不到任何变化:按照下面的例子,因为我已经更新了我的班,不粗体。无论使用什么字体,我的TextView都不会做任何更改,并且会使用默认的android字体进行显示。我想知道为什么 ?

我已经在博客文章here on my blog上总结了一切,也许它会帮助别人。

+0

谢谢为详细的问题和阐述,以及伟大的博客文章!为我完美工作。我也为同样的结果分类了Button。我唯一的问题是w.r.t.调用createFromAsset()_every_时间的效率。将字体加载一次并将它们存储在Application类中,并从MyTextView.setTypeface()访问这些字体会更好吗? –

+0

谢谢你的话。我也想过,但没有测试看它是如何工作的。它应该工作正常。无论如何,我还没有看到在屏幕上有许多意见的任何惩罚。 – Alin

回答

24

TextView的构造函数调用setTypeface(Typeface tf, int style),其中style参数从XML属性android:textStyle中检索。所以,如果你想截获此调用,迫使自己的字体,你可以重写此方法如下:

public void setTypeface(Typeface tf, int style) { 
    Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_normal_font.ttf"); 
    Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_bold_font.ttf"); 

    if (style == Typeface.BOLD) { 
     super.setTypeface(boldTypeface/*, -1*/); 
    } else { 
     super.setTypeface(normalTypeface/*, -1*/); 
    } 
} 
+0

你的解决方案看起来是正确的,但它不起作用.... – Alin

+2

@Alin:问题是''setTypeface()'在创建'字体'之前被构造函数调用!所以,在'setTypeface()'里面移动'Typeface'的创建,现在它就可以工作了! –

+0

现在,它的工作,谢谢。你已经度过了我的一天。 – Alin

9

你可以用我CustomTextView它允许你在你的assets文件夹指定字体文件名:

https://github.com/mafshin/CustomTextView

和使用是非常简单的:

<com.my.app.CustomTextView 
     xmlns:custom="http://schemas.android.com/apk/res/com.my.app"    
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Test text" 
     android:id="@+id/testcustomview" 

     custom:fontAssetName="Politica XT.otf" 
     /> 
2

我觉得这是更好地创造Ë自己的包自定义字体,并将其导入您的项目,这样就可以在未来

package com.codeslips.utilities; 

    import android.content.Context; 
    import android.graphics.Typeface; 
    import android.util.AttributeSet; 
    import android.widget.TextView; 

    public class CustomTextView extends TextView { 

      public CustomTextView(Context context) 
       { super(context); setFont(); } 

      public CustomTextView(Context context,AttributeSet set) 
      { super(context,set); setFont(); } 

      public CustomTextView(Context context,AttributeSet set,int defaultStyle) 
      { super(context,set,defaultStyle); setFont(); } 

      private void setFont() { 

      Typeface typeface=Typeface.createFromAsset(getContext().getAssets(),"fonts/your-font.ttf"); 
      setTypeface(typeface); //function used to set font 

      } 
      } 

在以后使用它们现在使用上面的类在XML文件中有您的自定义字体

<com.codeslips.utilities.CustomTextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Upload Image" 
     android:paddingTop="10sp" 
     android:textSize="14sp" 
     android:layout_weight="0.7" 
     android:textColor="@android:color/white"/>