[英]How to set a font for specific textview in Android?
我跟着從台階上這個運行時崩潰我的應用程序。 下面是我的整個代碼。
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView tx = (TextView)findViewById(R.id.textView5);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myriad_pro_regular.ttf");
tx.setTypeface(tf);
setContentView(R.layout.activity_main);
}
}
當我添加以下代碼時
TextView tx = (TextView)findViewById(R.id.textView5);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myriad_pro_regular.ttf");
tx.setTypeface(tf);
該應用程序崩潰了,我不知道我在這里做錯了什么。
您在初始化 textView 后設置了內容視圖。 這應該是正確的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView tx = (TextView)findViewById(R.id.textView5);
Typeface tf =
Typeface.createFromAsset(getAssets(),"fonts/myriad_pro_regular.ttf");
tx.setTypeface(tf);
}
首先將您的文本視圖初始化放在setContentView
下。
這是工作代碼
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView tx = (TextView)findViewById(R.id.text);
Typeface tf = Typeface.createFromAsset(getResources().getAssets(),"myriad_pro_regular.ttf");
tx.setTypeface(tf);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.