【问题标题】:Multiple fonts for multiple languages多种语言的多种字体
【发布时间】:2018-04-10 03:40:58
【问题描述】:

我最近在开发应用程序时遇到了一种情况,我必须在文本视图中显示不同的语言。目前我使用 fonts/typeface 显示很少这样的:

Typeface tf = Typeface.createFromAsset(this.getAssets(),
                "DroidHindi.ttf");
        TextView textView1 = (TextView) findViewById(R.id.textView2);
        textView1.setTypeface(tf);
        textView1.setText("कचजड, कचजड");

        Typeface tf1 = Typeface.createFromAsset(this.getAssets(),
                "asunaskh.ttf");
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setTypeface(tf1);
        textView.setText("یہ انگریزی نہیں");

        Typeface tf2 = Typeface.createFromAsset(this.getAssets(),
                "Banglafont.ttf");
        TextView textView2 = (TextView) findViewById(R.id.textView3);
        textView2.setTypeface(tf2);// এই ইংরেজি নয়
        textView2.setText("এই ইংরেজি নয়");

很好我的问题是我必须支持大约 20 种不同的语言,然后当我在不同的活动中应用它时,事情会变得非常乏味。任何替代方式来实现。

【问题讨论】:

    标签: android textview android-fonts


    【解决方案1】:

    您要做的是创建一个具有公共访问权限的类和一个函数,它将返回您想要假设的字体的 TextView 对象。

        TextView public SetLanguage(TextView tv,string type)
        {
         TextView newtv = tv;
         Typeface tf;
         switch(type)
         {
          case "urdu":
             tf = Typeface.createFromAsset(this.getAssets(),"urdu.ttf");
             break;
          case "hindi":
             tf = Typeface.createFromAsset(this.getAssets(),"hindi.ttf");
             break;
         //   up so on                
    
       }
       newtv.setTypeface(tf);
       return newtv;
     }
      // and call it any where..
     TextView textView1 = (TextView) findViewById(R.id.textView2);
     textView1 = classobj.SetLanguage(textView1,"urdu");
     //assign string of text to it
    

    【讨论】:

      【解决方案2】:

      在应用启动时初始化你的字体,并创建一个方法来获取任何视图并根据语言设置字体。

      【讨论】:

      • 正是我在我的应用程序中所做的,完美
      【解决方案3】:

      就像我们对其他资源文件所做的那样,我们也可以为我们想要的任何语言环境或国家代码定义一个字体目录。这种方法的缺点是两种字体都必须命名相同,尽管它们不同,但在其他方面是一个干净的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-16
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-27
        • 2014-08-16
        相关资源
        最近更新 更多