您好,我想为整个 Android 应用程序设置相同的字体系列。我看过这个Is it possible to set a custom font for entire of application?但这一切都在使用代码。我想在 Theme
中进行设置,使其在所有主题中都能正常工作。它还应该转换 ActionBar
文本的字体系列。
有什么办法吗?
样式.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
最佳答案
如果您想为所有 View 设置字体而不必每次都以编程方式进行(并且仍然适用于所有 Android 版本),您最好的选择是将 View 子类化并让它自动设置字体你想要的。
IE.
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init(boolean bold) {
setTypeface(Typeface.createFromAsset(getAssets(), "fonts/your_font_file.ttf"));
}
}
如果你真的想进一步优化它,你可以使用对该 TypeFace 的静态引用并使用它,这样你就不需要在每次加载 View 时都重新创建 TypeFace。
关于android-actionbar - 如何为整个Android应用程序设置相同的字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23135238/