改变字体类型与按钮在java



我正在尝试做一个自定义字体类型的按钮;在Android Studio中使用Java。我有一些字体类型(Simonetta_regular, Simonetta_black, Simonetta_black_italic, Simonetta_italic),我有一个按钮和一个textView。我想我的textview的字体类型改变,当我点击按钮。我想如果我把我的字体类型列出来,我就能做到。我该怎么做呢?

有很多方法,其中之一是:

TextView textview = (TextView) findViewById(R.id.textview);
Typeface tf= Typeface.createFromAsset(getAssets(),"fonts/Tahoma.ttf");
textview.setTypeface(tf);

,但如果你想设置默认字体,你可以这样做:

public class MyButton extends Button {

public MyButton(Context context) {
super(context);
init();
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
if (!isInEditMode()){
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"fonts/Tahoma.ttf");
this.setTypeface(tf);
}
}
}

最新更新