基于语言的Android textview字体 [英] Android textview font based on language
问题描述
是否有一种通用的字体处理方法,例如,如果是中文,则使用自定义字体,如果是英语,则返回默认字体?谢谢.
Is there a general method for handling font like, if it is Chinese I use a custom font for it, if English then I fall back to the default font? Thanks.
推荐答案
这是我的2美分.您可以使用书法库轻松更改整个应用程序或特定TextView
的自定义字体,如下所示:好吧.
Here is my 2 cents. You can use Calligraphy library to change the custom font easily either for whole app or for specific TextView
as well.
-
如果您想在整个应用程序中都这样做,那么我想到的一种解决方案是:
If you want to do it for whole app, then one solution I can think of is:
- 在您的应用中,您可以收听
广播 ="https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED" rel ="nofollow noreferrer"> ACTION_LOCALE_CHANGED . - 在侦听器内部检查区域设置是
Locale.SIMPLIFIED_CHINESE
还是Locale.TRADITIONAL_CHINESE
,然后更改应用程序的字体.
- In your app you can listen to the Broadcast of ACTION_LOCALE_CHANGED.
- Inside the listener check to see whether the locale is
Locale.SIMPLIFIED_CHINESE
orLocale.TRADITIONAL_CHINESE
, then change the app's font.
If you want to programatically change font of lets say partial text then you can use CalligraphyTypefaceSpan
as mentioned Multiple Typeface's per TextView / Spannables section:
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append("Hello!") // Bold this
.append("I use Calligraphy"); // Default TextView font.
// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new
CalligraphyTypefaceSpan(TypefaceUtils.load(getAssets(), "fonts/Roboto-Bold.ttf"));
// Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic.
sBuilder.setSpan(typefaceSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(sBuilder, TextView.BufferType.SPANNABLE);
希望有帮助.
附注:我不以任何方式隶属于图书馆.我很高兴.
这篇关于基于语言的Android textview字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!