对不适用于 Lollipop 的应用程序中的所有文本使用自定义字体
Using custom font for all text within app not working with Lollipop
我遇到的问题是手机和平板电脑 (Nexus 9) 上的 Android 5.0(包括 5.0.1/5.0.2)所特有的。 Android 的早期版本工作正常。
在我的应用程序中,我想设置一个覆盖所有文本的全局字体。我在 5.0 之前完成此操作的方法是使用 this method。这种字体覆盖方法似乎不适用于我尝试过的任何版本的 Lollipop,但在 2.x 和 4.x 中完美运行。我还在 BaseApplication
class 中运行此代码,所以字体仅在 BaseApplication
.
的 onCreate()
中初始化
这似乎是 Developer Preview 中的错误并已报告 here。我尝试了 post #16 中建议的修复方法,使用 TTX 工具将您的字体文件转换为 .ttx 然后再转换回 .otf,但这似乎并没有像其他人那样解决问题。我还根据 OTS 清理工具验证了我的 .otf 字体文件有效且未损坏。
我还有一个可以通过布局设置字体的自定义 TextView。一个例子是:
<com.myapp.widgets.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing text view font"
myapp:fontName="my-font.otf" />
此 CustomTextView 在视图的初始化中使用 setTypeface(typeFace)
来设置字体。这适用于 5.0,但实际上不是我可以使用的可能解决方案,因为我需要遍历每个布局并更改 TextView、EditText 等以使用 CustomTextView 并且不适用于 Dialog 文本枯萎。
因此,使用我的 CustomTextView class 设置单个 TextView 的字体在 Android 的所有版本中都可以正常工作,只是不在全局范围内设置它。
我还查看了样式源代码,看看是否可以找到 Material 和 Holo 主题之间的任何差异,但似乎不会改变 android:typeface
。我最初的想法是,Material 主题以某种方式覆盖了我的应用程序主题的 android:typeface
属性,但我找不到任何东西。
任何想法或意见将不胜感激,谢谢!
我最终解决这个问题的方法是使用 Calligraphy 设置全局字体。 https://github.com/chrisjenx/Calligraphy
只需将此代码添加到我的自定义应用程序 class 的 onCreate()
。
// Custom font file located in the "assets/fonts/"
String customFont = "Helvetica-Neue.otf";
CalligraphyConfig.initDefault(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/" + customFont)
.build()
);
从 SDK v26 开始更新
如果您只支持 v26+ 或使用支持库,我们现在提供了一种内置方法来处理整个应用程序中的自定义字体。这里有一个link to the Android Developers page。基本情况如下:
1) 将自定义字体(.otf 或 .ttf)添加到 res/font
目录
2) 使用您添加的字体文件创建一个新的字体系列:res/font/your_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Normal -->
<font
android:font="@font/your_font_normal"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/your_font_normal"
app:fontStyle="normal"
app:fontWeight="400"/>
<!-- Italic -->
<font
android:font="@font/your_font_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/your_font_italic"
app:fontStyle="italic"
app:fontWeight="400"/>
<!-- Bold -->
<font
android:font="@font/your_font_bold"
android:fontStyle="normal"
android:fontWeight="700"
app:font="@font/your_font_bold"
app:fontStyle="normal"
app:fontWeight="700"/>
</font-family>
3) 如果您想在整个应用程序中应用此字体,请在您的基本主题样式中创建一个样式,通常位于:res/styles/style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Other app styling -->
<item name="android:fontFamily">@font/your_font_family</item>
</style>
4) 如果您只想将字体应用于单个或少数视图,您可以使用 XML 中的 android:fontFamily
属性或使用:
val typeface = ResourcesCompat.getFont(context, R.font.your_font_family)
textView.typeface = typeface
这似乎适用于我测试过的所有 Android 版本。
我也在关注此 method which worked until Android 4.4 like a charm, but didn't work in Lolipop. In my case, the problem was solved (after some googling) by converting the ".ttf" font file to ".otf" I used this web 进行转换。现在它适用于所有版本。希望对你有帮助
我遇到的问题是手机和平板电脑 (Nexus 9) 上的 Android 5.0(包括 5.0.1/5.0.2)所特有的。 Android 的早期版本工作正常。
在我的应用程序中,我想设置一个覆盖所有文本的全局字体。我在 5.0 之前完成此操作的方法是使用 this method。这种字体覆盖方法似乎不适用于我尝试过的任何版本的 Lollipop,但在 2.x 和 4.x 中完美运行。我还在 BaseApplication
class 中运行此代码,所以字体仅在 BaseApplication
.
onCreate()
中初始化
这似乎是 Developer Preview 中的错误并已报告 here。我尝试了 post #16 中建议的修复方法,使用 TTX 工具将您的字体文件转换为 .ttx 然后再转换回 .otf,但这似乎并没有像其他人那样解决问题。我还根据 OTS 清理工具验证了我的 .otf 字体文件有效且未损坏。
我还有一个可以通过布局设置字体的自定义 TextView。一个例子是:
<com.myapp.widgets.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing text view font"
myapp:fontName="my-font.otf" />
此 CustomTextView 在视图的初始化中使用 setTypeface(typeFace)
来设置字体。这适用于 5.0,但实际上不是我可以使用的可能解决方案,因为我需要遍历每个布局并更改 TextView、EditText 等以使用 CustomTextView 并且不适用于 Dialog 文本枯萎。
因此,使用我的 CustomTextView class 设置单个 TextView 的字体在 Android 的所有版本中都可以正常工作,只是不在全局范围内设置它。
我还查看了样式源代码,看看是否可以找到 Material 和 Holo 主题之间的任何差异,但似乎不会改变 android:typeface
。我最初的想法是,Material 主题以某种方式覆盖了我的应用程序主题的 android:typeface
属性,但我找不到任何东西。
任何想法或意见将不胜感激,谢谢!
我最终解决这个问题的方法是使用 Calligraphy 设置全局字体。 https://github.com/chrisjenx/Calligraphy
只需将此代码添加到我的自定义应用程序 class 的 onCreate()
。
// Custom font file located in the "assets/fonts/"
String customFont = "Helvetica-Neue.otf";
CalligraphyConfig.initDefault(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/" + customFont)
.build()
);
从 SDK v26 开始更新
如果您只支持 v26+ 或使用支持库,我们现在提供了一种内置方法来处理整个应用程序中的自定义字体。这里有一个link to the Android Developers page。基本情况如下:
1) 将自定义字体(.otf 或 .ttf)添加到 res/font
目录
2) 使用您添加的字体文件创建一个新的字体系列:res/font/your_font_family.xml
<?xml version="1.0" encoding="utf-8"?>
<font-family
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Normal -->
<font
android:font="@font/your_font_normal"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/your_font_normal"
app:fontStyle="normal"
app:fontWeight="400"/>
<!-- Italic -->
<font
android:font="@font/your_font_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/your_font_italic"
app:fontStyle="italic"
app:fontWeight="400"/>
<!-- Bold -->
<font
android:font="@font/your_font_bold"
android:fontStyle="normal"
android:fontWeight="700"
app:font="@font/your_font_bold"
app:fontStyle="normal"
app:fontWeight="700"/>
</font-family>
3) 如果您想在整个应用程序中应用此字体,请在您的基本主题样式中创建一个样式,通常位于:res/styles/style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Other app styling -->
<item name="android:fontFamily">@font/your_font_family</item>
</style>
4) 如果您只想将字体应用于单个或少数视图,您可以使用 XML 中的 android:fontFamily
属性或使用:
val typeface = ResourcesCompat.getFont(context, R.font.your_font_family)
textView.typeface = typeface
这似乎适用于我测试过的所有 Android 版本。
我也在关注此 method which worked until Android 4.4 like a charm, but didn't work in Lolipop. In my case, the problem was solved (after some googling) by converting the ".ttf" font file to ".otf" I used this web 进行转换。现在它适用于所有版本。希望对你有帮助