【问题标题】:Assigning Custom Font to TextView [duplicate]将自定义字体分配给 TextView [重复]
【发布时间】:2018-01-18 03:28:00
【问题描述】:

我是编程新手,我希望将我下载的自定义字体添加到 Android Studio。我能够按照说明如何添加字体,但是当我运行应用程序时,我只能让我的两个 TextView 中的一个 TextView 使用此字体。这是我的代码,有人可以告诉我我在这里做错了什么。谢谢!

public class MainActivity extends AppCompatActivity {
TextView text, text2;
    Typeface tfc1, tfc2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.top_text);
        text2 = (TextView) findViewById(R.id.bottom_text);

        tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
        text.setTypeface(tfc1);

        tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
        text2.setTypeface(tfc2);



    }
}

【问题讨论】:

    标签: android fonts


    【解决方案1】:

    我认为有一个错字。你写了 tfc1 但你设置了 tfc2。从您的代码中:

    tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
                text2.setTypeface(tfc2);  
    

    因为 tfc1tfc2 字体都是一样的。正如@Sagar Patel 所示,您可以对两个文本视图使用一种字体。

    【讨论】:

      【解决方案2】:

      第 1 步/

      首先创建一个名为 assests 的文件夹,然后在该文件夹中创建另一个名为 folder 的文件夹并将您的 *.ttf 文件导入该文件夹

      第 2 步/

      现在在开始编写下面给出的代码之前导入它:

      import android.graphics.Typeface;
      

      现在在你的类中实现以下代码:

      // Font path
      String fontPath = "fonts/Face Your Fears.ttf";
      
      // text view label
      TextView txtGhost = (TextView) findViewById(R.id.ghost);
      
      // Loading Font Face
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
      
      // Applying font
      txtGhost.setTypeface(tf);
      

      我建议你按照这个教程right here 一步步指导如何在 Android Studio 中使用外部字体

      【讨论】:

      • 我在这些步骤中遇到了同样的问题。我只能让我的 TextView id= top_text 使用字体。
      【解决方案3】:

      本教程link 有助于了解如何在 android 中配置自定义字体。

      【讨论】:

        【解决方案4】:

        您可以在xml文件中使用android:fontFamily属性。

        【讨论】:

        • 只适用于minSDK >= 21
        • 是的。那么您使用的是哪个 minversion?
        【解决方案5】:
        public class MainActivity extends AppCompatActivity {
        TextView text, text2;
            Typeface tfc1, tfc2;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                text = (TextView) findViewById(R.id.top_text);
                text2 = (TextView) findViewById(R.id.bottom_text);
        
                tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
                text.setTypeface(tfc1);
        
              //  tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
             //you get error in this line   text2.setTypeface(tfc2);
        
               text2.setTypeface(tfc1);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-11
          • 2013-06-28
          • 2013-08-23
          • 2011-11-06
          • 1970-01-01
          相关资源
          最近更新 更多