What is the equivalent of “android:fontFamily=”sans-serif-light" in Java code?

后端 未结 6 1805
半阙折子戏
半阙折子戏 2020-12-23 15:46

My question is quite simple:

In every of my textview, I am currently using the attribute

android:fontFamily=\"sans-serif-light\"

6条回答
  •  孤独总比滥情好
    2020-12-23 16:04

    It should be possible with setTypeface() and Typeface.create():

    convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
    

    See Docs:

    Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (getStyle()) to discover what its "real" style characteristics are.

    Note that excessively using Typeface.create() is bad for your memory, as stated in this comment. The suggested Hashtable is a good solution, but you have to modify it a little since you don't create your typeface from an asset.

提交回复
热议问题