简体   繁体   English

如何在Android中将字体设置为textview?

[英]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 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. 我正在使用.otf文件。 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 请注意,文件扩展名是“ ttf”,用于在Google中搜索要在此扩展名中下载的任何字体

for example: http://www.creamundo.com/ 例如: 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 在代码中,您使用fonts/CURLZ_.otf作为路径,但是您说字体文件直接位于资源中,因此您的代码应为

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() 1)将字体放在资源文件夹中,而不是字体文件夹中2)在代码中使用getthis.getAssets()或youractivity.this.getAssets()代替getAssests()

Thank You 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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