2013-11-20 71 views
2

我想添加自定义字体到我的android应用程序。这是我的代码。谁能告诉我我哪里错了?添加自定义字体到Android应用程序

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView textview = (TextView) findViewById(R.id.FontTest); 

     Typeface tf = Typeface.createFromAsset(getApplicationContext() 
       .getAssets(), "fonts/snap-itc.ttf"); 
     textview.setTypeface(tf); 
    } 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 
    <TextView 
     android:id="@+id/FontTest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Font Test" 
     android:textSize="50px"/> 
</RelativeLayout> 
+0

的代码是罚款 –

+0

代码看起来不错 – Ridcully

+0

的代码是好的...是ü得到任何错误或异常后的那 – nitesh

回答

2

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/snap-itc.ttf"); 

代替:

Typeface tf = Typeface.createFromAsset(getApplicationContext() 
       .getAssets(), "fonts/snap-itc.ttf"); 
+0

不能再次工作。错误是“本地字体无法设置” – user3008254

+1

只将您的ttf文件置于资产文件夹中。然后将其作为'Typeface tf = Typeface.createFromAsset(getAssets(),“snap-itc.ttf”);'。 –

0

很可能是显而易见的,但要确保你有一个/资产/字体 FOL der在您的项目目录中,并且您的自定义字体在那里。另外,请检查字体的正确拼写(+是大写还是小写),并更正代码中的拼写。

这也可能意味着字体本身已损坏。

0

在应用程序中添加字体像

TextView txtvw=(TextView)findViewById(R.id.textviewname); 

Typeface typface=Typeface.createFromAsset(getAssets(),"fonts/burmese.ttf"); 

    txtvw.setTypeface(typface); 
相关问题