问题描述
- Android textView 中 onCreate 方法的改变
-
我在main.xml上使用 Layout Editor创建了一个textview名称是textView1。
我想使用一个自定义的字体,所以我在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); } }
什么原因呢?
解决方案
TextView textView1=(TextView)findViewById(R.id.textView1);//mail.xml里这个textview的id
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf");
textView1.setTypeface(type);
解决方案二:
请初始化 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);
}
时间: 2024-09-23 07:29:50