启动画面中的FontType

时间:2015-10-23 18:49:31

标签: android android-typeface

我试图在启动画面上更改字体系列。

public class SplashActivity extends AppCompatActivity {


/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);

    /* New Handler to start the Menu-Activity
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(SplashActivity.this, ApresentationActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);


}

我已经尝试过使用typeFace,但没有成功。 我可以在不使用savedInstanceState的情况下使用字体吗?

TextView myTextView=(TextView)findViewById(R.id.textBox); Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf"); myTextView.setTypeface(typeFace);

编辑1:

package com.example.jelbez.json;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.widget.TextView;

public class SplashActivity extends AppCompatActivity {

TextView txtTitle;

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);

    /* New Handler to start the Menu-Activity
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(SplashActivity.this, ApresentationActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);


    TextView myTextView=(TextView)findViewById(R.id.txtTitle);
    Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
    myTextView.setTypeface(typeFace);
}



}

这不起作用,它返回我引起:java.lang.RuntimeException:无法生成本机字体

1 个答案:

答案 0 :(得分:0)

你应该在像这样的处理程序之前编写textview和字体

TextView myTextView=(TextView)findViewById(R.id.txtTitle);
Typeface typeFace= Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        /* Create an Intent that will start the Menu-Activity. */
        Intent mainIntent = new Intent(SplashActivity.this, ApresentationActivity.class);
        SplashActivity.this.startActivity(mainIntent);
        SplashActivity.this.finish();
    }
}, SPLASH_DISPLAY_LENGTH);

如果不起作用,请检查资产中的字体文件夹 再次,如果不工作,改变onCreate并使用此

public class SplashActivity extends AppCompatActivity {


private final int SPLASH_DISPLAY_LENGTH = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

TextView myTextView=(TextView)findViewById(R.id.txtTitle);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent mainIntent = new Intent(SplashActivity.this, ApresentationActivity.class);
        SplashActivity.this.startActivity(mainIntent);
        SplashActivity.this.finish();
    }
}, SPLASH_DISPLAY_LENGTH);
}