基于语言的Android textview字体 [英] Android textview font based on language

查看:169
本文介绍了基于语言的Android textview字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种通用的字体处理方法,例如,如果是中文,则使用自定义字体,如果是英语,则返回默认字体?谢谢.

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:

  1. 在您的应用中,您可以收听广播 ="https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED" rel ="nofollow noreferrer"> ACTION_LOCALE_CHANGED .
  2. 在侦听器内部检查区域设置是Locale.SIMPLIFIED_CHINESE还是Locale.TRADITIONAL_CHINESE,然后更改应用程序的字体.
  1. In your app you can listen to the Broadcast of ACTION_LOCALE_CHANGED.
  2. Inside the listener check to see whether the locale is Locale.SIMPLIFIED_CHINESE or Locale.TRADITIONAL_CHINESE, then change the app's font.

  • 如果要以编程方式更改部分文字的字体,则可以使用CalligraphyTypefaceSpan,如所述

  • 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆