您可能想要创建包含所有字体的 static class . 这样,您不会多次创建可能会对 performance 造成严重影响的字体 . 只需确保创建名为“ fonts " under " assets ”文件夹的 sub-folder .
做类似的事情:
public class CustomFontsLoader {
public static final int FONT_NAME_1 = 0;
public static final int FONT_NAME_2 = 1;
public static final int FONT_NAME_3 = 2;
private static final int NUM_OF_CUSTOM_FONTS = 3;
private static boolean fontsLoaded = false;
private static Typeface[] fonts = new Typeface[3];
private static String[] fontPath = {
"fonts/FONT_NAME_1.ttf",
"fonts/FONT_NAME_2.ttf",
"fonts/FONT_NAME_3.ttf"
};
/**
* Returns a loaded custom font based on it's identifier.
*
* @param context - the current context
* @param fontIdentifier = the identifier of the requested font
*
* @return Typeface object of the requested font.
*/
public static Typeface getTypeface(Context context, int fontIdentifier) {
if (!fontsLoaded) {
loadFonts(context);
}
return fonts[fontIdentifier];
}
private static void loadFonts(Context context) {
for (int i = 0; i < NUM_OF_CUSTOM_FONTS; i++) {
fonts[i] = Typeface.createFromAsset(context.getAssets(), fontPath[i]);
}
fontsLoaded = true;
}
}
这样,您就可以从应用程序的任何位置获取字体 .
32
有史以来的最佳实践
TextViewPlus.java:
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface typeface = null;
try {
typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Unable to load typeface: "+e.getMessage());
return false;
}
setTypeface(typeface);
return true;
}
}
public class Font {
public static final Font PROXIMA_NOVA = new Font("ProximaNovaRegular.otf");
public static final Font FRANKLIN_GOTHIC = new Font("FranklinGothicURWBoo.ttf");
private final String assetName;
private volatile Typeface typeface;
private Font(String assetName) {
this.assetName = assetName;
}
public void apply(Context context, TextView textView) {
if (typeface == null) {
synchronized (this) {
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), assetName);
}
}
}
textView.setTypeface(typeface);
}
}
16 回答
首先,默认值不是Arial . 默认是Droid Sans .
其次,要更改为不同的内置字体,请在布局XML中使用
android:typeface
或在Java中使用setTypeface()
.第三,Android中没有Helvetica字体 . 内置的选择是Droid Sans(
sans
),Droid Sans Mono(monospace
)和Droid Serif(serif
) . 虽然您可以将自己的字体与应用程序捆绑在一起并通过setTypeface()
使用它们,但请记住,字体文件很大,在某些情况下,需要许可协议(例如,Helvetica, a Linotype font) .EDIT
在ICS之后,android包含Roboto字体样式,阅读更多Roboto
EDIT 2
首先下载所需字体的
.ttf
文件(arial.ttf
) . 将其放在assets
文件夹中 . (内部资源文件夹创建名为 fonts 的新文件夹并将其放入其中 . )使用以下代码将字体应用于TextView
:您可能想要创建包含所有字体的 static class . 这样,您不会多次创建可能会对 performance 造成严重影响的字体 . 只需确保创建名为“ fonts " under " assets ”文件夹的 sub-folder .
做类似的事情:
这样,您就可以从应用程序的任何位置获取字体 .
有史以来的最佳实践
TextViewPlus.java:
attrs.xml: (何处放置 res/values )
How to use:
希望这会帮助你 .
上面的答案是正确的 . 如果您正在使用该段代码,请确保在“assets”文件夹下创建一个名为“fonts”的子文件夹 .
整合字体创建的另一种方法......
然后在你的活动中使用......
请注意,这个带有volatile字段的双重检查锁定习惯只能与Java 1.5中使用的内存模型一起正常工作 .
最佳做法是使用Android支持库版本26.0.0或更高版本 .
STEP 1: add font file
在res文件夹中创建新的字体资源字典
添加字体文件(.ttf,.orf)
例如,当字体文件为helvetica_neue.ttf时将生成R.font.helvetica_neue
STEP 2: create font family
在font文件夹中添加新资源文件
将元素中的每个字体文件,样式和权重属性括起来 .
例如:
STEP 3: use it
在xml布局中:
或者将字体添加到样式:
有关更多示例,您可以按照文档:
它有点旧,但我改进了类CustomFontLoader一点点,我想分享它,所以它可以是有帮助的 . 只需使用此代码创建一个新类 .
然后在textview上使用此代码:
这样,您可以创建一个继承自TextView并在其构造函数上调用setTypeface的View .
我终于得到了一个非常简单的解决方案 .
然后在 res 文件夹中创建名为 "font" 的目录
将字体(ttf)文件放在该字体目录中,请记住命名约定[egname不应包含任何特殊字符,任何大写字符和任何空格或制表符]
之后,像这样从 xml 引用该字体
在此示例中, times_new_roman_test 是该字体目录中的字体ttf文件
从资产中获取字体并设置为所有子项
也许有点简单:
(注意这段代码是未经测试的,但一般来说这种方法应该运行良好 . )
当你的字体存储在里面
res/asset/fonts/Helvetica.ttf
使用以下内容:或者,如果您的字体文件存储在
res/font/helvetica.ttf
内,请使用以下命令:Android使用Roboto字体,这是一种非常漂亮的字体,具有几种不同的权重(常规,轻,薄,浓缩),在高密度屏幕上看起来很棒 .
检查以下链接以检查roboto字体:
How to use Roboto in xml layout
回到你的问题,如果你想更改 all of the TextView/Button in your app 的字体,请尝试将以下代码添加到styles.xml中以使用 Roboto-light font:
不要忘记在AndroidManifest.xml中使用“AppTheme”