如何在Android应用程序中添加字体

5

我在开发一个Android 2.2应用程序,在应用程序中我想在TextView中显示印地语文本。(如何将“मैं छात्र हूँ”这个词添加到TextView中)

请帮助我。


可能是以下问题的重复:如何将外部字体添加到Android应用程序中 - Ted Hopp
2个回答

17
将字体添加到项目的资产文件夹中,并使用以下代码片段。
 TextView hindiTextView=(TextView)findViewById(R.id.txtbx);
 Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
 mytextView.setTypeface(hindiFont);

希望这对你有所帮助。


它不起作用,只显示矩形框,我已经包含了vigyapti.ttf文件。 - Paresh Mayani
字体文件夹出现错误,应该把它放在哪里?我已经放在资源文件夹里了。 - Giridharan
你必须像@Jana所说的那样将其包含在资产文件夹中。 - lagos
2
如果我想在整个应用程序中使用这个字体怎么办?有没有办法在清单文件中提及字体? - reiley

3

1) 在 "assets" 文件夹内添加一个名为 "fonts" 的文件夹,并将字体粘贴到 "fonts" 文件夹中。

2) 在布局文件中:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
             android:orientation="vertical"  
              android:layout_width="fill_parent"  
              android:layout_height="fill_parent"  
        >  

    <TextView  
           android:id="@+id/custom_font"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="मैं छात्र हूँ"  
           />  

3) 在您希望设置文本的活动中,请使用以下代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接