Android Typeface createFromAsset

后端 未结 7 2124
滥情空心
滥情空心 2020-12-14 14:49

I Have a Custom View which draws text onto the Canvas. I want to change the font to a font stored in the assets folder.

I

相关标签:
7条回答
  • 2020-12-14 15:01

    In order to reuse typefaces in my projects I create a class full of typeface methods, this way I dont have to create a new typeface every time.

    I call the class FontClass and in the class there is a method for each typeface I need to use e.g:

    public static Typeface getOpenSansRegular(Context c){
        return Typeface.createFromAsset(c.getAssets(), "OpenSans-Light.ttf");
    }
    

    Then I can use them like so:

    TextView text = (TextView) findViewById(R.id.textview);
    text.setTypeface(FontClass.getOpenSansRegular(getApplicationContext());
    
    0 讨论(0)
  • 2020-12-14 15:07

    My mistake was adding the line

    Typeface mFont = Typeface.createFromAsset(this.getAssets(), "abc.ttf");
    

    before onCreate()

    0 讨论(0)
  • created a folder src/main/assets and placed font files in there.

    in Activity

    Typeface font = Typeface.createFromAsset(getAssets(),  "Mukta-Regular.ttf");
    tv.setTypeface(font);
    

    in Fragment

    Typeface.createFromAsset(getActivity().getAssets(), "Mukta-Regular.ttf");
    tv.setTypeface(font);
    
    0 讨论(0)
  • 2020-12-14 15:15
        Typeface robo = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
    
    0 讨论(0)
  • 2020-12-14 15:20

    First of all, you have to keep your assets folder inside your project and not inside src/main.. And then, create a folder called fonts inside assets. then, put the specific font typeface ttf files inside it.You can use the font typeface in coding like:

    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/filename.ttf");
    textview.setTypeface(type);
    
    0 讨论(0)
  • 2020-12-14 15:22

    You can use your View's getContext() method to get the current Context, then use it to get the assets:

    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "robotobold.ttf");
    
    0 讨论(0)
提交回复
热议问题