简体   繁体   中英

how to set font to textview in android?

I am doing like this but it get force close. I want to change textview font .

TextView mWeddingDataandTime=(TextView)findViewById(R.id.wedding_weddingtime_txt);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/CURLZ_.otf"); 
mWeddingDataandTime.setTypeface(face); 

I am using .otf file. It is in assets folder. Any idea?

Try this:

    TextView txt = (TextView) findViewById(R.id.custom_font);
    Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
    txt.setTypeface(font);

Note that file extension is "ttf" search in google for any font for download in this extension

for example: http://www.creamundo.com/

In code you are using fonts/CURLZ_.otf as path but you say that font file is directly inside assets so your code should be

Typeface face=Typeface.createFromAsset(getAssets(),"CURLZ_.otf"); 

if there is no fonts folder inside assets folder.

I didn't test it but I think it may help you:

  TextView mWeddingDataandTime=(TextView)findViewById(R.id.wedding_weddingtime_txt);
  Typeface face=Typeface.createFromAsset(getAssets(),"fonts/CURLZ_"); 
  mWeddingDataandTime.setTypeface(face); 

更好地利用样式和主题以及内置字体来解决与性能相关的问题以及与应用相关的问题的大小样式与主题

如果您的字体仅位于资产文件夹中,而不位于asser / fonts中,则请使用此字体

 Typeface face=Typeface.createFromAsset(getAssets(),"CURLZ_.otf"); 

Below is code which I used in my project & it's working... You might need to do some changes

  Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),"segoeuil.ttf");
  TextView loantext = (TextView)findViewById(R.id.loantext);
  length1.setTypeface(myTypeface);

1) Put font in assets folder instead of fonts folders 2) instead of getAssests() in you code use "this.getAssets()" or "youractivity.this.getAssets()

Thank You

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM