2012-01-09 52 views

回答

-1

我希望这是你的意思,但如果不是,它应该是其他人的一个很好的参考。

**注:字体可以发现电脑/本地磁盘(C:)/窗/字体**

复制,你想在上面的字体文件夹中使用,并将其粘贴到新的字体在Eclipse中的资产文件夹中创建文件夹。

private void initTypeFace() 
    { 
     TypeFace tf = TypeFace.createFromAsset(getAsset(), 
         "Chantelli Antiqua.ttf"); 

     TextView txt = (TextView) findViewById(R.id.custom_font); 

     txt.setTypeface(tf); 
     example_button1.setTypeface(tf); 
     example_button2.setTypeface(tf); 
    } 
0

我认为您的问题的答案会是自定义的TextView与额外的XML参数,您可以将其添加到您的主题。

你可以在构造函数TextView(Context context,AttributeSet attrs)中解析这个值来初始化它。检查this链接,例如为您的视图定义自定义attrs并初始化它们。

12

我认为这是this questionthis one的重复。

在我运行的活动,我用的是这样的:

FontUtils.setCustomFont(findViewById(R.id.top_view), getAssets()); 

在XML:

 <TextView 
      android:id="@+id/my_label" 
      android:tag="condensed" 
      android:text="@string/label" 
      ... /> 

所以理论上你可以创建的风格和FontUtils /运行时代码一起使用。

<style name="roboto_condensed"> 
    <item name="android:tag">condensed,your-own-css-like-language-here</item> 
</style> 

FontUtils类:

public class FontUtils { 
    private static Typeface normal; 

    private static Typeface bold; 

    private static Typeface condensed; 

    private static Typeface light; 

    private static void processsViewGroup(ViewGroup v, final int len) { 

    for (int i = 0; i < len; i++) { 
     final View c = v.getChildAt(i); 
     if (c instanceof TextView) { 
     setCustomFont((TextView) c); 
     } else if (c instanceof ViewGroup) { 
     setCustomFont((ViewGroup) c); 
     } 
    } 
    } 

    private static void setCustomFont(TextView c) { 
    Object tag = c.getTag(); 
    if (tag instanceof String) { 
     if (((String) tag).contains("bold")) { 
     c.setTypeface(bold); 
     return; 
     } 
     if (((String) tag).contains("condensed")) { 
     c.setTypeface(condensed); 
     return; 
     } 
     if (((String) tag).contains("light")) { 
     c.setTypeface(light); 
     return; 
     } 
    } 
    c.setTypeface(normal); 
    } 

    public static void setCustomFont(View topView, AssetManager assetsManager) { 
    if (normal == null || bold == null || condensed == null || light == null) { 
     normal = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Regular.ttf"); 
     bold = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Bold.ttf"); 
     condensed = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Condensed.ttf"); 
     light = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Light.ttf"); 
    } 

    if (topView instanceof ViewGroup) { 
     setCustomFont((ViewGroup) topView); 
    } else if (topView instanceof TextView) { 
     setCustomFont((TextView) topView); 
    } 
    } 

    private static void setCustomFont(ViewGroup v) { 
    final int len = v.getChildCount(); 
    processsViewGroup(v, len); 
    } 
} 
3

您可以在资产文件夹中的自定义字体类型,并从那里retreive它。

声明字样为:

Typeface helveticaBold; 
Typeface helveticaRegular; 
中的onCreate

()写入以下代码:

helveticaBold = Typeface.createFromAsset(getAssets(), "helvetica_bold.ttf"); 
helveticaRegular = Typeface.createFromAsset(getAssets(), "helvetica_regular.ttf"); 

最后,设置的TextView或EditText上的文本作为的字体:

editText.setTypeface(helveticaRegular); 

就是这样......

+0

肯定的,但是不知道他在问 – user1010160 2015-03-18 09:51:44

4

通过使用我的CustomTextView,您可以在您的XML布局文件中直接在assets文件夹中指定一个字体文件名。

我的回答是here

19

不幸的是,Android不提供你正在寻找改变字体为您的整个应用程序的快捷,方便,清洁的方式。但最近我研究了这个问题,并创建了一些工具,允许您在没有任何编码的情况下更改字体(您可以通过xml,样式甚至文本显示完成所有工作)。他们基于类似的解决方案,就像你在这里的其他答案中看到的一样,但考虑到更多的灵活性。你可以阅读关于this blog的所有信息,并参阅github项目here

下面是如何应用这些工具的例子。把所有的字体文件放在assets/fonts/。然后,将这些字体声明为xml文件(例如res/xml/fonts.xml),并在您的应用程序中早期加载该文件,并使用TypefaceManager.initialize(this, R.xml.fonts);(例如,在您的Application类的onCreate中)。该XML文件是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<familyset> 

    <!-- Some Font. Can be referenced with 'someFont' or 'aspergit' --> 
    <family> 
     <nameset> 
      <name>aspergit</name> 
      <name>someFont</name> 
     </nameset> 
     <fileset> 
      <file>Aspergit.ttf</file> 
      <file>Aspergit Bold.ttf</file> 
      <file>Aspergit Italic.ttf</file> 
      <file>Aspergit Bold Italic.ttf</file> 
     </fileset> 
    </family> 

    <!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' --> 
    <family> 
     <nameset> 
      <name>bodoni</name> 
      <name>anotherFont</name> 
     </nameset> 
     <fileset> 
      <file>BodoniFLF-Roman.ttf</file> 
      <file>BodoniFLF-Bold.ttf</file> 
     </fileset> 
    </family> 

</familyset> 

现在你可以使用你的风格或XML(前提是你用我上面提到的工具)这些字体,在你的XML布局设置自定义的TextView com.innovattic.font.FontTextView的flFont属性。下面你可以看到如何在您的整个应用程序的字体的所有文本,仅仅通过编辑res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> 

    <!-- Application theme --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
     <item name="android:textViewStyle">@style/MyTextViewStyle</item> 
    </style> 

    <!-- Style to use for ALL text views (including FontTextView) --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView"> 
     <item name="android:textAppearance">@style/MyTextAppearance</item> 
    </style> 

    <!-- Text appearance to use for ALL text views (including FontTextView) --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo"> 
     <!-- Alternatively, reference this font with the name "aspergit" --> 
     <!-- Note that only our own TextView's will use the font attribute --> 
     <item name="flFont">someFont</item> 
     <item name="android:textStyle">bold|italic</item> 
    </style> 

    <!-- Alternative style, maybe for some other widget --> 
    <style name="StylishFont"> 
     <item name="flFont">anotherFont</item> 
     <item name="android:textStyle">normal</item> 
    </style> 

</resources> 

并结合res/layout/layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <!-- This text view is styled with the app theme --> 
    <com.innovattic.font.FontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This uses my font in bold italic style" /> 

    <!-- This text view is styled here and overrides the app theme --> 
    <com.innovattic.font.FontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:flFont="anotherFont" 
     android:textStyle="normal" 
     android:text="This uses another font in normal style" /> 

    <!-- This text view is styled with a style and overrides the app theme --> 
    <com.innovattic.font.FontTextView 
     style="@style/StylishFont" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This also uses another font in normal style" /> 

</LinearLayout> 

不要忘记申请主题在你的Android清单中。

+0

的感谢! GREAT文章.. – 2014-11-15 04:00:53

+0

由于您使用的是java代码,因此我们可以以更简单的方式执行 – 2015-12-30 10:20:37

+0

这真的应该是android中的native – Zach 2016-09-16 18:50:37