Using custom font in android TextView using xml

前端 未结 10 1081
执念已碎
执念已碎 2020-11-29 17:02

I have added a custom font file to my assets/fonts folder. How do I use it from my XML?

I can use it from code as follows:

TextView text =          


        
10条回答
  •  日久生厌
    2020-11-29 17:48

    I know this is an old question, but i've found a much easier solution.

    First declare your TextView in xml as usual. Put your font (TTF or TTC) in the asset folder

    app\src\main\assets\

    Then just set the typeface for your text view in your onCreate method.

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name);    
    
        TextView textView = findViewById(R.id.my_textView);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "fontName.ttf");
        textView.setTypeface(typeface);
    }
    

    Done.

提交回复
热议问题