修改应用内字体样式

2017-09-04  本文已影响0人  诠_释

首先在自定义的BaseApplication中设置如下代码:

public class BaseApplication extends Application {

@Override

public void onCreate() {

Typeface typeFaceYaHei = Typeface.createFromAsset(getAssets(),

"fonts/test.ttf");

try {

Field field = Typeface.class.getDeclaredField("SERIF");

field.setAccessible(true);

field.set(null, typeFaceYaHei);

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

super.onCreate();

}

}

然后在AndroidManifest.xml中application标签中的theme属性:style对应中添加该行

<item name="android:typeface">serif</item>

但该方法,不能改变Toast中的文字 样式 ,要改变文字样式,需要在toast实例时,自定义view的属性

private voidshowToast(String info) {

if(toast==null) {

toast= Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT);

LinearLayout layout = (LinearLayout)toast.getView();

layout.setBackgroundColor(Color.parseColor("#FFFFFF"));

TextView v = (TextView)toast.getView().findViewById(android.R.id.message);

v.setTextColor(Color.BLACK);

Typeface typeFaceYaHei = Typeface.createFromAsset(getAssets(),

"fonts/Adobe_Heiti_Std_R.TTF");

v.setTypeface(typeFaceYaHei);

v.setTextSize(25);

}else{

toast.setText(info);

}

toast.show();

}

上一篇下一篇

猜你喜欢

热点阅读