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);
}
AssetManager mgr = getAssets(); Typeface tf = Typeface.createFromAsset(mgr, "fonts/NotoSansCJKsc-Black.otf"); tv_1.setTypeface(tf);
public class CustomTextView extends TextView
{
public CustomTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
//重写设置字体方法
@Override
public void setTypeface(Typeface tf)
{
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/NotoSansCJKsc-Light.otf");
super.setTypeface(tf);
}
}
<com.test.fontfamily.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="自定义字体"
android:textSize="24dp"
/>
package com.test.fontfamily;
import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2017/10/24.
*/
public class FontUtils
{
private static final String TAG = FontUtils.class.getSimpleName();
private Map<String, SoftReference<Typeface>> mCache = new HashMap<>();
private static FontUtils sSingleton = null;
public static Typeface DEFAULT = Typeface.DEFAULT;
// disable instantiate
private FontUtils()
{
}
public static FontUtils getInstance()
{
// double check
if (sSingleton == null)
{
synchronized (FontUtils.class)
{
if (sSingleton == null)
{
sSingleton = new FontUtils();
}
}
}
return sSingleton;
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root The root view.
* @param fontPath font file path relative to 'assets' directory.
*/
public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath)
{
replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath));
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root The root view.
* @param fontPath font file path relative to 'assets' directory.
* @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC}
*/
public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style)
{
replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style);
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root The root view.
* @param fontPath The full path to the font data.
*/
public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath)
{
replaceFont(root, createTypefaceFromFile(fontPath));
}
/**
* <p>Replace the font of specified view and it's children</p>
*
* @param root The root view.
* @param fontPath The full path to the font data.
* @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC}
*/
public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style)
{
replaceFont(root, createTypefaceFromFile(fontPath), style);
}
/**
* <p>Replace the font of specified view and it's children with specified typeface</p>
*/
private void replaceFont(@NonNull View root, @NonNull Typeface typeface)
{
if (root == null || typeface == null)
{
return;
}
if (root instanceof TextView)
{ // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView) root;
// Extract previous style of TextView
int style = Typeface.NORMAL;
if (textView.getTypeface() != null)
{
style = textView.getTypeface().getStyle();
}
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup)
{ // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i)
{
replaceFont(viewGroup.getChildAt(i), typeface);
}
} // else return
}
/**
* <p>Replace the font of specified view and it's children with specified typeface and text style</p>
*
* @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC}
*/
private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style)
{
if (root == null || typeface == null)
{
return;
}
if (style < 0 || style > 3)
{
style = Typeface.NORMAL;
}
if (root instanceof TextView)
{ // If view is TextView or it's subclass, replace it's font
TextView textView = (TextView) root;
textView.setTypeface(typeface, style);
} else if (root instanceof ViewGroup)
{ // If view is ViewGroup, apply this method on it's child views
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); ++i)
{
replaceFont(viewGroup.getChildAt(i), typeface, style);
}
} // else return
}
/**
* <p>Create a Typeface instance with specified font file</p>
*
* @param fontPath font file path relative to 'assets' directory.
* @return Return created typeface instance.
*/
private Typeface createTypefaceFromAsset(Context context, String fontPath)
{
SoftReference<Typeface> typefaceRef = mCache.get(fontPath);
Typeface typeface = null;
if (typefaceRef == null || (typeface = typefaceRef.get()) == null)
{
typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
typefaceRef = new SoftReference<>(typeface);
mCache.put(fontPath, typefaceRef);
}
return typeface;
}
private Typeface createTypefaceFromFile(String fontPath)
{
SoftReference<Typeface> typefaceRef = mCache.get(fontPath);
Typeface typeface = null;
if (typefaceRef == null || (typeface = typefaceRef.get()) == null)
{
typeface = Typeface.createFromFile(fontPath);
typefaceRef = new SoftReference<>(typeface);
mCache.put(fontPath, typefaceRef);
}
return typeface;
}
/**
* <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p>
* {@code <item name="android:typeface">monospace</item>}
* <p>The best place to call this method is {@link Application#onCreate()}, it will affect
* whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p>
*
* @param context {@link Context Context}
* @param fontPath font file path relative to 'assets' directory.
*/
public void replaceSystemDefaultFontFromAsset(@NonNull Context context, @NonNull String fontPath)
{
replaceSystemDefaultFont(createTypefaceFromAsset(context, fontPath));
}
/**
* <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p>
* {@code <item name="android:typeface">monospace</item>}
* <p>The best place to call this method is {@link Application#onCreate()}, it will affect
* whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p>
*
* @param context {@link Context Context}
* @param fontPath The full path to the font data.
*/
public void replaceSystemDefaultFontFromFile(@NonNull Context context, @NonNull String fontPath)
{
replaceSystemDefaultFont(createTypefaceFromFile(fontPath));
}
/**
* <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p>
* {@code <item name="android:typeface">monospace</item>}
* <p>The best place to call this method is {@link Application#onCreate()}, it will affect
* whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p>
*/
private void replaceSystemDefaultFont(@NonNull Typeface typeface)
{
modifyObjectField(null, "MONOSPACE", typeface);
}
private void modifyObjectField(Object obj, String fieldName, Object value)
{
try
{
Field defaultField = Typeface.class.getDeclaredField(fieldName);
defaultField.setAccessible(true);
defaultField.set(obj, value);
} catch (NoSuchFieldException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}
FontUtils.getInstance().replaceFontFromAsset(View root, String fontPath)
FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this,"fonts/NotoSansCJKsc-Thin.otf");
<item name="android:typeface">monospace</item>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有