这是对该问题的跟进:。我有一些自定义字体,我将它们添加到资产/字体文件夹中。现在我想使用该库中的字体作为整个应用程序的默认字体。上面链接问题的被接受答案建议使用一个样式,这正是我想要的,但需要使用属性
"android:fontFamily"
,该属性仅适用于API 16及以上版本。是否有方法也可以在旧版本API中使用样式?"android:fontFamily"
,该属性仅适用于API 16及以上版本。是否有方法也可以在旧版本API中使用样式?对于 "android:fontFamily"
:
- 只接受由您的操作系统版本提供的默认字体LINK。
- 在 Android "O" 之后,您可以使用您的资源文件夹LINK。
因此,如果您有现有的项目,最好和更快的设置应用程序中的字体的方法是使用@Oussaki建议的Calligraphy Library。
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fontPath="fonts/Roboto-Bold.ttf"/>
在集成后,只需要添加fontPath="fonts/Roboto-Bold.ttf"
即可。
同时也适用于Eclipse .Aar文件。
不要忘记在每个根布局中添加此行。LINK
tools:ignore="MissingPrefix"
快乐编程 :)
public class TypefaceUtil
{
/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
*
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets)
{
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("serif", customFontTypeface);
try
{
final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
}
catch (NoSuchFieldException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
else
{
try
{
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e)
{
Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
}
然后我只需在Application
类中添加这一行:
@Override
public void onCreate()
{
super.onCreate();
TypefaceUtil.overrideFont(getApplicationContext(), "serif", "fonts/Ubuntu-Regular.ttf");
}
fonts
目录保存字体文件,位于src\main
目录下的assets
目录内(src\main\assets\fonts
)
在你的资产资源中添加字体ttf文件
尝试使用下面的自定义文本视图类,并将自定义类添加到你的xml文件中
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
this.setTypeface(getTypeFace(context));
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(getTypeFace(context));
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setTypeface(getTypeFace(context));
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.setTypeface(getTypeFace(context));
}
public Typeface getTypeFace(Context context) {
Typeface font = Typeface.createFromAsset(context.getAssets(), "your_filename.ttf");
return font;
}
}
在 XML 文件中,例如:
<package_name.CustomTextView
android:id="@+id/tv_downloading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/icon_close"
android:textSize="100dp"
android:textStyle="bold"
android:layout_centerInParent="true"
android:background="@color/col_white"
android:textColor="@color/colorPrimaryDark"
android:visibility="visible" />