如何在Android中更改TextView的fontFamily

所以我想在Android中更改android:fontFamily ,但在Android中没有看到任何预定义的字体。 我如何select一个预定义的? 我并不需要定义自己的TypeFace,但是我需要的是与现在所显示的不同的东西。

 <TextView android:id="@+id/HeaderText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="52dp" android:gravity="center" android:text="CallerBlocker" android:textSize="40dp" android:fontFamily="Arial" /> 

看来我做了什么不会真的工作! 顺便说一句android:fontFamily="Arial"是一个愚蠢的尝试!

从android 4.1 / 4.2 / 5.0开始,下列Roboto字体系列可用:

 android:fontFamily="sans-serif" // roboto regular android:fontFamily="sans-serif-light" // roboto light android:fontFamily="sans-serif-condensed" // roboto condensed android:fontFamily="sans-serif-black" // roboto black android:fontFamily="sans-serif-thin" // roboto thin (android 4.2) android:fontFamily="sans-serif-medium" // roboto medium (android 5.0) 

在这里输入图像描述

与…结合

 android:textStyle="normal|bold|italic" 

这16个变种是可能的:

  • Roboto经常
  • Roboto斜体
  • Roboto大胆
  • Roboto粗体斜体
  • 的Roboto光强
  • Roboto-Light斜体
  • 的Roboto薄
  • Roboto薄斜体
  • 的Roboto冷凝
  • Roboto-Condensed斜体
  • Roboto-Condensed bold
  • Roboto-Condensed粗斜体
  • 的Roboto黑
  • Roboto黑色斜体
  • 的Roboto介质
  • Roboto中等斜体

fonts.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="font_family_light">sans-serif-light</string> <string name="font_family_medium">sans-serif-medium</string> <string name="font_family_regular">sans-serif</string> <string name="font_family_condensed">sans-serif-condensed</string> <string name="font_family_black">sans-serif-black</string> <string name="font_family_thin">sans-serif-thin</string> </resources> 

这是以编程方式设置字体的方法:

 TextView tv = (TextView) findViewById(R.id.appname); Typeface face = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf"); tv.setTypeface(face); 

把字体文件放在资产文件夹中。 在我的情况下,我创build了一个名为字体的子目录。

编辑:如果你想知道你的资产文件夹在哪里看到这个问题

我不得不在最近的项目中parsing/system/etc/fonts.xml 。 以下是棒棒糖的当前字体系列:

 ╔════╦════════════════════════════╦═════════════════════════════╗ ║ ║ FONT FAMILY ║ TTF FILE ║ ╠════╬════════════════════════════╬═════════════════════════════╣ ║ 1 ║ casual ║ ComingSoon.ttf ║ ║ 2 ║ cursive ║ DancingScript-Regular.ttf ║ ║ 3 ║ monospace ║ DroidSansMono.ttf ║ ║ 4 ║ sans-serif ║ Roboto-Regular.ttf ║ ║ 5 ║ sans-serif-black ║ Roboto-Black.ttf ║ ║ 6 ║ sans-serif-condensed ║ RobotoCondensed-Regular.ttf ║ ║ 7 ║ sans-serif-condensed-light ║ RobotoCondensed-Light.ttf ║ ║ 8 ║ sans-serif-light ║ Roboto-Light.ttf ║ ║ 9 ║ sans-serif-medium ║ Roboto-Medium.ttf ║ ║ 10 ║ sans-serif-smallcaps ║ CarroisGothicSC-Regular.ttf ║ ║ 11 ║ sans-serif-thin ║ Roboto-Thin.ttf ║ ║ 12 ║ serif ║ NotoSerif-Regular.ttf ║ ║ 13 ║ serif-monospace ║ CutiveMono.ttf ║ ╚════╩════════════════════════════╩═════════════════════════════╝ 

这里是parsing器(基于FontListParser ):

 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.Xml; /** * Helper class to get the current font families on an Android device.</p> * * Usage:</p> {@code List<SystemFont> fonts = FontListParser.safelyGetSystemFonts();}</p> */ public final class FontListParser { private static final File FONTS_XML = new File("/system/etc/fonts.xml"); private static final File SYSTEM_FONTS_XML = new File("/system/etc/system_fonts.xml"); public static List<SystemFont> getSystemFonts() throws Exception { String fontsXml; if (FONTS_XML.exists()) { fontsXml = FONTS_XML.getAbsolutePath(); } else if (SYSTEM_FONTS_XML.exists()) { fontsXml = SYSTEM_FONTS_XML.getAbsolutePath(); } else { throw new RuntimeException("fonts.xml does not exist on this system"); } Config parser = parse(new FileInputStream(fontsXml)); List<SystemFont> fonts = new ArrayList<>(); for (Family family : parser.families) { if (family.name != null) { Font font = null; for (Font f : family.fonts) { font = f; if (f.weight == 400) { break; } } SystemFont systemFont = new SystemFont(family.name, font.fontName); if (fonts.contains(systemFont)) { continue; } fonts.add(new SystemFont(family.name, font.fontName)); } } for (Alias alias : parser.aliases) { if (alias.name == null || alias.toName == null || alias.weight == 0) { continue; } for (Family family : parser.families) { if (family.name == null || !family.name.equals(alias.toName)) { continue; } for (Font font : family.fonts) { if (font.weight == alias.weight) { fonts.add(new SystemFont(alias.name, font.fontName)); break; } } } } if (fonts.isEmpty()) { throw new Exception("No system fonts found."); } Collections.sort(fonts, new Comparator<SystemFont>() { @Override public int compare(SystemFont font1, SystemFont font2) { return font1.name.compareToIgnoreCase(font2.name); } }); return fonts; } public static List<SystemFont> safelyGetSystemFonts() { try { return getSystemFonts(); } catch (Exception e) { String[][] defaultSystemFonts = { { "cursive", "DancingScript-Regular.ttf" }, { "monospace", "DroidSansMono.ttf" }, { "sans-serif", "Roboto-Regular.ttf" }, { "sans-serif-light", "Roboto-Light.ttf" }, { "sans-serif-medium", "Roboto-Medium.ttf" }, { "sans-serif-black", "Roboto-Black.ttf" }, { "sans-serif-condensed", "RobotoCondensed-Regular.ttf" }, { "sans-serif-thin", "Roboto-Thin.ttf" }, { "serif", "NotoSerif-Regular.ttf" } }; List<SystemFont> fonts = new ArrayList<>(); for (String[] names : defaultSystemFonts) { File file = new File("/system/fonts", names[1]); if (file.exists()) { fonts.add(new SystemFont(names[0], file.getAbsolutePath())); } } return fonts; } } /* Parse fallback list (no names) */ public static Config parse(InputStream in) throws XmlPullParserException, IOException { try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(in, null); parser.nextTag(); return readFamilies(parser); } finally { in.close(); } } private static Alias readAlias(XmlPullParser parser) throws XmlPullParserException, IOException { Alias alias = new Alias(); alias.name = parser.getAttributeValue(null, "name"); alias.toName = parser.getAttributeValue(null, "to"); String weightStr = parser.getAttributeValue(null, "weight"); if (weightStr == null) { alias.weight = 0; } else { alias.weight = Integer.parseInt(weightStr); } skip(parser); // alias tag is empty, ignore any contents and consume end tag return alias; } private static Config readFamilies(XmlPullParser parser) throws XmlPullParserException, IOException { Config config = new Config(); parser.require(XmlPullParser.START_TAG, null, "familyset"); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } if (parser.getName().equals("family")) { config.families.add(readFamily(parser)); } else if (parser.getName().equals("alias")) { config.aliases.add(readAlias(parser)); } else { skip(parser); } } return config; } private static Family readFamily(XmlPullParser parser) throws XmlPullParserException, IOException { String name = parser.getAttributeValue(null, "name"); String lang = parser.getAttributeValue(null, "lang"); String variant = parser.getAttributeValue(null, "variant"); List<Font> fonts = new ArrayList<Font>(); while (parser.next() != XmlPullParser.END_TAG) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); if (tag.equals("font")) { String weightStr = parser.getAttributeValue(null, "weight"); int weight = weightStr == null ? 400 : Integer.parseInt(weightStr); boolean isItalic = "italic".equals(parser.getAttributeValue(null, "style")); String filename = parser.nextText(); String fullFilename = "/system/fonts/" + filename; fonts.add(new Font(fullFilename, weight, isItalic)); } else { skip(parser); } } return new Family(name, fonts, lang, variant); } private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException { int depth = 1; while (depth > 0) { switch (parser.next()) { case XmlPullParser.START_TAG: depth++; break; case XmlPullParser.END_TAG: depth--; break; } } } private FontListParser() { } public static class Alias { public String name; public String toName; public int weight; } public static class Config { public List<Alias> aliases; public List<Family> families; Config() { families = new ArrayList<Family>(); aliases = new ArrayList<Alias>(); } } public static class Family { public List<Font> fonts; public String lang; public String name; public String variant; public Family(String name, List<Font> fonts, String lang, String variant) { this.name = name; this.fonts = fonts; this.lang = lang; this.variant = variant; } } public static class Font { public String fontName; public boolean isItalic; public int weight; Font(String fontName, int weight, boolean isItalic) { this.fontName = fontName; this.weight = weight; this.isItalic = isItalic; } } public static class SystemFont { public String name; public String path; public SystemFont(String name, String path) { this.name = name; this.path = path; } } } 

随意在您的项目中使用上述类。 例如,您可以为您的用户提供多种字体系列,并根据他们的偏好设置字体。

一个不完整的例子:

 final List<FontListParser.SystemFont> fonts = FontListParser.safelyGetSystemFonts(); String[] items = new String[fonts.size()]; for (int i = 0; i < fonts.size(); i++) { items[i] = fonts.get(i).name; } new AlertDialog.Builder(this).setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { FontListParser.SystemFont selectedFont = fonts.get(which); // TODO: do something with the font Toast.makeText(getApplicationContext(), selectedFont.path, Toast.LENGTH_LONG).show(); } }).show(); 

Android不允许您从XML布局设置自定义字体。 相反,您必须将特定字体文件捆绑到应用程序的资产文件夹中,然后以编程方式进行设置。 就像是:

 TextView textView = (TextView) findViewById(<your TextView ID>); Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>"); textView.setTypeface(typeFace); 

请注意,只能在调用setContentView()之后才能运行此代码。 另外,Android只支持一些字体,并且应该是.ttf (TrueType).otf (OpenType)格式。 即使如此,一些字体可能无法正常工作。

这是一种绝对适用于Android的字体,您可以使用此字体来确认您的代码是否正常工作,以防您的字体文件不受Android支持。

Android O更新:基于Roger的评论,现在可以在Android O中使用XML 。

这和android:typeface

内置的字体是:

  • 正常
  • SANS
  • 衬线
  • 等宽

请参阅android:字体 。

以编程方式设置Roboto:

 paint.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL)); 

如果你想以编程方式,你可以使用

 label.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC); 

你可以在哪里使用SANS_SERIF

  • DEFAULT
  • DEFAULT_BOLD
  • MONOSPACE
  • SANS_SERIF
  • SERIF

而你可以在哪里使用ITALIC

  • BOLD
  • BOLD_ITALIC
  • ITALIC
  • NORMAL

所有内容均在Android开发人员中声明

我正在使用由Chris Jenxdevise的优秀图书馆书法 ,允许您在android应用程序中使用自定义字体。 试一试!

你想要的是不可能的。 您必须在代码中设置TypeFace

XML你可以做的是

 android:typeface="sans" | "serif" | "monospace" 

除此之外,你不能在XML中使用字体。 🙂

对于Arial您需要在代码中设置types面。

pipe理字体的简单方法是通过资源来声明它们,例如:

 <!--++++++++++++++++++++++++++--> <!--added on API 16 (JB - 4.1)--> <!--++++++++++++++++++++++++++--> <!--the default font--> <string name="fontFamily__roboto_regular">sans-serif</string> <string name="fontFamily__roboto_light">sans-serif-light</string> <string name="fontFamily__roboto_condensed">sans-serif-condensed</string> <!--+++++++++++++++++++++++++++++--> <!--added on API 17 (JBMR1 - 4.2)--> <!--+++++++++++++++++++++++++++++--> <string name="fontFamily__roboto_thin">sans-serif-thin</string> <!--+++++++++++++++++++++++++++--> <!--added on Lollipop (LL- 5.0)--> <!--+++++++++++++++++++++++++++--> <string name="fontFamily__roboto_medium">sans-serif-medium</string> <string name="fontFamily__roboto_black">sans-serif-black</string> <string name="fontFamily__roboto_condensed_light">sans-serif-condensed-light</string> 

这是基于这里这里的源代码

Android-Studio 3.0开始,它很容易改变字体家族

使用支持库26,它将在运行Android API版本14和更高版本的设备上工作

res目录下创build一个文件夹font下载你想要的字体并将其粘贴到font文件夹中。 结构应该是下面的一些东西

这里

创build您自己的字体系列

右键单击字体文件夹并转到新build>字体资源文件 。 出现“新build资源文件”窗口。

input文件名称 ,然后单击确定 。 新的字体资源XML在编辑器中打开。

例如,在这里写你自己的字体系列

 <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/lobster_regular" /> <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/lobster_italic" /> </font-family> 

1.更改 布局中的 fontfamily,您可以编写

  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/lobster"/> 

2.编程方式更改

  Typeface typeface = getResources().getFont(R.font.myfont); textView.setTypeface(typeface); 

请参阅文档了解更多信息

我制作了一个名为Foundry的小库,您可以使用它来通过XML布局和样式应用自定义字体。

随着一些试验和错误,我学到了以下内容。

在* .xml中,您可以将库存字体与以下函数结合使用,而不仅仅是字体:

  android:fontFamily="serif" android:textStyle="italic" 

有了这两种风格,在任何其他情况下都不需要使用字体。 fontFamily和textStyle组合的范围更大。

android:fontFamily的有效值在/system/etc/system_fonts.xml(4.x)或/system/etc/fonts.xml(5.x)中定义。 但设备制造商可能会对其进行修改,因此设置fontFamily值所使用的实际字体取决于指定设备的上述文件。

在AOSP中,Arial字体是有效的,但必须使用“arial”而不是“Arial”来定义,例如android:fontFamily =“arial” 。 在Kitkat的system_fonts.xml中看看qucik

  <family> <nameset> <name>sans-serif</name> <name>arial</name> <name>helvetica</name> <name>tahoma</name> <name>verdana</name> </nameset> <fileset> <file>Roboto-Regular.ttf</file> <file>Roboto-Bold.ttf</file> <file>Roboto-Italic.ttf</file> <file>Roboto-BoldItalic.ttf</file> </fileset> </family> 

////////////////////////////////////////////////// ////////////////////////

在layout- android:fontFamilyandroid:typefaceandroid:textStyle中定义了一个“字体”,有三个相关的xml属性。 “fontFamily”和“textStyle”或“typeface”和“textStyle”的组合可用于改变文本中字体的外观,因此单独使用。 TextView.java中的代码片段如下所示:

  private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) { Typeface tf = null; if (familyName != null) { tf = Typeface.create(familyName, styleIndex); if (tf != null) { setTypeface(tf); return; } } switch (typefaceIndex) { case SANS: tf = Typeface.SANS_SERIF; break; case SERIF: tf = Typeface.SERIF; break; case MONOSPACE: tf = Typeface.MONOSPACE; break; } setTypeface(tf, styleIndex); } public void setTypeface(Typeface tf, int style) { if (style > 0) { if (tf == null) { tf = Typeface.defaultFromStyle(style); } else { tf = Typeface.create(tf, style); } setTypeface(tf); // now compute what (if any) algorithmic styling is needed int typefaceStyle = tf != null ? tf.getStyle() : 0; int need = style & ~typefaceStyle; mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0); mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0); } else { mTextPaint.setFakeBoldText(false); mTextPaint.setTextSkewX(0); setTypeface(tf); } } 

从代码我们可以看到:

  1. 如果设置了“fontFamily”,则“字体”将被忽略。
  2. “字体”有标准和有限的有效值。 实际上,这些值是“normal”,“sans”,“serif”和“monospace”,它们可以在system_fonts.xml(4.x)或fonts.xml(5.x)中find。 其实“正常”和“无”是系统的默认字体。
  3. “fontFamily”可用于设置内置字体的所有字体,而“字体”仅提供“sans-serif”“serif”和“monospace”(世界三大字体types)的典型字体, 。
  4. 当只设置“textStyle”时,我们实际设置了默认的字体和指定的风格。 有效值是“正常”“大胆”“斜体”和“粗体”。
 <string name="font_family_display_4_material">sans-serif-light</string> <string name="font_family_display_3_material">sans-serif</string> <string name="font_family_display_2_material">sans-serif</string> <string name="font_family_display_1_material">sans-serif</string> <string name="font_family_headline_material">sans-serif</string> <string name="font_family_title_material">sans-serif-medium</string> <string name="font_family_subhead_material">sans-serif</string> <string name="font_family_menu_material">sans-serif</string> <string name="font_family_body_2_material">sans-serif-medium</string> <string name="font_family_body_1_material">sans-serif</string> <string name="font_family_caption_material">sans-serif</string> <string name="font_family_button_material">sans-serif-medium</string> 

dynamic的,你可以使用这个在xml中设置类似于android:fontFamily的fontfamily,

 For Custom font: TextView tv = ((TextView) v.findViewById(R.id.select_item_title)); Typeface face=Typeface.createFromAsset(getAssets(),"fonts/mycustomfont.ttf"); tv.setTypeface(face); For Default font: tv.setTypeface(Typeface.create("sans-serif-medium",Typeface.NORMAL)); 

这些是所使用的默认字体系列的列表,使用任何这个replace双引号string“sans-serif-medium”

 FONT FAMILY TTF FILE 1 casual ComingSoon.ttf 2 cursive DancingScript-Regular.ttf 3 monospace DroidSansMono.ttf 4 sans-serif Roboto-Regular.ttf 5 sans-serif-black Roboto-Black.ttf 6 sans-serif-condensed RobotoCondensed-Regular.ttf 7 sans-serif-condensed-light RobotoCondensed-Light.ttf 8 sans-serif-light Roboto-Light.ttf 9 sans-serif-medium Roboto-Medium.ttf 10 sans-serif-smallcaps CarroisGothicSC-Regular.ttf 11 sans-serif-thin Roboto-Thin.ttf 12 serif NotoSerif-Regular.ttf 13 serif-monospace CutiveMono.ttf 

“mycustomfont.ttf”是ttf文件。 path将在src / assets / fonts / mycustomfont.ttf中 ,您可以参考有关默认字体的更多信息

这也是一个好的库RobotoTextView 。 它确实服务于您的需求。

你可以在res/layout/value/style.xml设置样式:

 <style name="boldText"> <item name="android:textStyle">bold|italic</item> <item name="android:textColor">#FFFFFF</item> </style> 

并在main.xml文件中使用此样式使用:

 style="@style/boldText" 

在某些情况下,这样做更容易 。 原则是在你的xml布局中添加一个不可见的TextVview,并在java代码中获得它的typeFace

xml文件中的布局:

  <TextView android:text="The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty." android:layout_width="0dp" android:layout_height="0dp" android:fontFamily="sans-serif-thin" android:id="@+id/textViewDescription"/> 

和java代码:

 myText.setTypeface(textViewSelectedDescription.getTypeface()); 

它已经为我工作(例如在一个TextSwitcher)。

如果你想在相同字体族的许多地方使用TextView,请扩展TextView类并设置你的字体:

 public class ProximaNovaTextView extends TextView { public ProximaNovaTextView(Context context) { super(context); applyCustomFont(context); } public ProximaNovaTextView(Context context, AttributeSet attrs) { super(context, attrs); applyCustomFont(context); } public ProximaNovaTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); applyCustomFont(context); } private void applyCustomFont(Context context) { Typeface customFont = FontCache.getTypeface("proximanova_regular.otf", context); setTypeface(customFont); } } 

然后在xml中使用这个自定义类,如下所示:

  <com.myapp.customview.ProximaNovaTextView android:id="@+id/feed_list_item_name_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" /> 

在这里你可以看到所有可用的fontFamily值和它对应的字体文件的名字(这个文件在android 5.0+中使用)。 在移动设备中,您可以在以下位置find它:

/system/etc/fonts.xml(适用于5.0+)

(对于使用这个版本的Android 4.4及以下版本,但我认为fonts.xml有一个更清晰的格式和易于理解。)

例如,

  <!-- first font is default --> 20 <family name="sans-serif"> 21 <font weight="100" style="normal">Roboto-Thin.ttf</font> 22 <font weight="100" style="italic">Roboto-ThinItalic.ttf</font> 23 <font weight="300" style="normal">Roboto-Light.ttf</font> 24 <font weight="300" style="italic">Roboto-LightItalic.ttf</font> 25 <font weight="400" style="normal">Roboto-Regular.ttf</font> 26 <font weight="400" style="italic">Roboto-Italic.ttf</font> 27 <font weight="500" style="normal">Roboto-Medium.ttf</font> 28 <font weight="500" style="italic">Roboto-MediumItalic.ttf</font> 29 <font weight="900" style="normal">Roboto-Black.ttf</font> 30 <font weight="900" style="italic">Roboto-BlackItalic.ttf</font> 31 <font weight="700" style="normal">Roboto-Bold.ttf</font> 32 <font weight="700" style="italic">Roboto-BoldItalic.ttf</font> 33 </family> 

family标签的名称属性name="sans-serif"定义了您可以在android:fontFamily中使用的值。

font标签定义相应的字体文件。

在这种情况下,您可以忽略<!-- fallback fonts -->下的源代码,它用于字体的后备逻辑。

我使用信件按lib为我的NonTextView东西像button和kianoni fontloader lib为我的TextViews在这个lib中使用风格的原因比信件更容易按我和我理想的反馈与此。 对于那些想要使用除Roboto字体外的自定义字体的人来说,这非常棒。 所以这是我的字体库的经验。 对于那些想要使用自定义类更改字体我强烈build议使用此代码段创build此类

 public class TypefaceSpan extends MetricAffectingSpan { /** An <code>LruCache</code> for previously loaded typefaces. */ private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(12); private Typeface mTypeface; /** * Load the {@link android.graphics.Typeface} and apply to a {@link android.text.Spannable}. */ public TypefaceSpan(Context context, String typefaceName) { mTypeface = sTypefaceCache.get(typefaceName); if (mTypeface == null) { mTypeface = Typeface.createFromAsset(context.getApplicationContext() .getAssets(), String.format("fonts/%s", typefaceName)); // Cache the loaded Typeface sTypefaceCache.put(typefaceName, mTypeface); } } @Override public void updateMeasureState(TextPaint p) { p.setTypeface(mTypeface); // Note: This flag is required for proper typeface rendering p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); } @Override public void updateDrawState(TextPaint tp) { tp.setTypeface(mTypeface); // Note: This flag is required for proper typeface rendering tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); } 

}

并使用这样的类

 AppData = PreferenceManager.getDefaultSharedPreferences(this); TextView bannertv= (TextView) findViewById(R.id.txtBanner); SpannableString s = new SpannableString(getResources().getString(R.string.enterkey)); s.setSpan(new TypefaceSpan(this, AppData.getString("font-Bold",null)), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); bannertv.setText(s); 

也许这个帮助。

您可以通过使用以下库来轻松完成

https://github.com/sunnag7/FontStyler

 <com.sunnag.fontstyler.FontStylerView android:textStyle="bold" android:text="@string/about_us" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="8dp" app:fontName="Lato-Bold" android:textSize="18sp" android:id="@+id/textView64" /> 

它的重量轻,易于实现,只需复制资产文件夹中的字体,并在XML中使用名称。

我只想提一下,Android里的字体地狱即将结束,因为今年在Google IO上,我们终于得到了这个 – > https://developer.android.com/preview/features/working-with-fonts。; HTML

现在有一个新的资源types的字体 ,你可以把所有的应用程序字体放在res / fonts文件夹中,然后用R.font.my_custom_font访问,就像你可以访问string res值, 绘制 res值等。你甚至有机会创build字体的 XML文件,这将是你的自定义字体设置(关于斜体,粗体和下划线attr)。

阅读上面的链接了解更多信息。 让我们看看支持。