0

我使用 main.xml 上的布局编辑器创建了一个名为 textView1 的 textView。我想使用自定义字体,所以我在 onCreate 中设置了一行代码,但它似乎无法识别名称 textView1

package com.mystraldesign.memorable;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;

public class MemorableActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 
        textView1.setTypeface(type);
    }
}

我确定我错过了一些简单的东西,但这是我第一次编写 Android 代码,所以仍然感觉我的方式。

4

2 回答 2

2
TextView textView1= (TextView) findViewById(R.id.text_info);
Typeface type= Typeface.createFromAsset(getAssets(),"fonts/optima.ttf");
textView1.setTypeface(type);
于 2012-04-28T05:32:10.607 回答
2

请在您的代码中初始化 TextView...

试试这个:-

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      // Font path
      String fontPath = "fonts/optima.ttf";
      // text view label
      TextView textView1= (TextView) findViewById(R.id.name);
     // Loading Font Face
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
     // Applying font
      textView1.setTypeface(tf);
  }
于 2012-04-28T05:38:08.803 回答