有两种存档方式
第一种方式
public class FontCache {
private static HashMap<String, Typeface> fontCache = new HashMap<>();
public static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), fontname);
} catch (Exception e) {
return null;
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
这会缓存字体,同时最大限度地减少对资产的访问次数。现在,既然我们有了访问自定义字体的方法,让我们实现一个扩展 TextView 的类。
扩展 TextView
接下来,我们将创建一个扩展 TextView 的新 Java 类。这允许我们在所有 XML 视图中使用该类。它继承了常规 TextView 的所有功能和属性;但添加了我们的自定义字体。
我们再次查看了我们的 eat foody 项目的源代码。代码可能看起来很复杂,但很简单:
public class EatFoodyTextView extends TextView {
public EatFoodyTextView(Context context) {
super(context);
applyCustomFont(context);
}
public EatFoodyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public EatFoodyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
Typeface customFont = FontCache.getTypeface("SourceSansPro-Regular.ttf", context);
setTypeface(customFont);
}
}
前三个方法只是构造函数,我们重写它们以调用单个方法 applyCustomFont()。该方法是难题的重要组成部分。它只是从我们的 FontCache 类中获取(希望缓存的)字体。最后,我们必须使用字体调用 setTypeface(),我们几乎完成了。如果您想知道,我们可以直接调用 setTypeface()(而不是在 TextView 对象上),因为我们正在扩展 TextView 类。
使用类
您可能想知道,如此多的准备是否值得付出努力。在本节中,您将看到确实如此。因为您剩下要做的就是在 XML 视图中使用该类,它会自动拥有您的自定义字体。不需要 Java 代码!
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.futurestudio.foody.views.EatFoodyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/eat_foody_green_dark"
android:textSize="20sp"
android:text="Future Studio Blog"
android:layout_marginBottom="24dp"/>
</RelativeLayout>
如您所见,您可以继续使用 TextView 的所有细节(例如 textSize、textColor)。现在,只需用我们刚刚创建的类替换所有元素,例如,您在任何地方都应用了自定义字体!
(参考:https://futurestud.io/tutorials/custom-fonts-on-android-extending-textview)
第二种方式
遵循 API 26 (Android 8) https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml 的 Google 指南支持
在 textview 之间进行更改以更改字体
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/eat_foody_green_dark"
android:textSize="20sp"
android:text="Future Studio Blog"
android:layout_marginBottom="24dp"/>
<com.futurestudio.foody.views.EatFoodyTextView
android:id="@+id/textview_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/eat_foody_green_dark"
android:textSize="20sp"
android:text="Future Studio Blog"
android:visibility="gone"
android:layout_marginBottom="24dp"/>
</RelativeLayout>
关注android:visibility="gone"
在 Activity 中,您使用此代码在 2 个 TextView 之间切换
final TextView normalTextView = findViewById(R.id.textview_normal);
final TextView customTextView = findViewById(R.id.textview_custom);
normalTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
normalTextView.setVisibility(View.GONE);
customTextView.setVisibility(View.VISIBLE);
}
});
customTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
normalTextView.setVisibility(View.VISIBLE);
customTextView.setVisibility(View.GONE);
}
});