在Android中将自定义字体添加到主题

有什么办法可以在Android中的主题添加自定义字体?

我已阅读快速提示:自定义Android字体 ,但在这里,我们必须程序化添加自定义字体的文本。

TextView txt = (TextView) findViewById(R.id.custom_font); Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf"); txt.setTypeface(font); 

但我想通过样式/主题设置自定义字体。

不幸的是,Android不提供你想要改变整个应用的字体的快速,简单和干净的方式。 但最近我已经研究了这个问题,并创build了一些工具,允许您更改字体,而无需任何编码(可以通过xml,样式甚至文本显示来完成)。 他们基于类似的解决scheme,就像你在这里的其他答案中看到的,但考虑到更多的灵活性。 你可以阅读这个博客的所有信息,并在这里看到github项目。

这是一个如何应用这些工具的例子。 把所有的字体文件放在assets/fonts/ 。 然后,在xml文件(例如res/xml/fonts.xml )中声明这些字体,并使用TypefaceManager.initialize(this, R.xml.fonts);在应用程序中尽早加载这个文件TypefaceManager.initialize(this, R.xml.fonts); (例如,在您的应用程序类的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中使用这些字体(只要使用上面提到的工具), com.innovattic.font.FontTextView在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清单中应用主题。

我认为这是这个问题和这个 问题的重复。

在我的运行时,我使用这样的东西:

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

在XML中:

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

所以理论上你可以创build风格,并将其与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); } } 

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

我的答案在这里

您可以将您的自定义字体types包含在资产文件夹中,并从那里进行检索。

声明字体为:

 Typeface helveticaBold; Typeface helveticaRegular; 

在onCreate()中写下面的代码:

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

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

 editText.setTypeface(helveticaRegular); 

而已…

我认为你的问题的答案会是自定义的TextView和额外的XML参数,你可以将其包含到你的主题中。

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

我希望这是你的意思,但如果不是这样的话,那么对其他人来说应该是一个很好的参考。

**注意:可以find字体计算机/本地磁盘(C:)/ Windows / Fonts **

在上面的字体文件夹中复制要使用的字体,并将其粘贴到Eclipse中assets文件夹中新创build的文件夹中。

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