Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改整个应用程序的字体_Android_Fonts_Styles_Custom Font - Fatal编程技术网

Android 更改整个应用程序的字体

Android 更改整个应用程序的字体,android,fonts,styles,custom-font,Android,Fonts,Styles,Custom Font,我想在我的应用程序中使用自定义字体。这是为整个应用程序提供字体的最佳方式。我知道如何为单个文本视图或按钮指定自定义字体 是否可以在一个地方提及自定义字体,例如在styles.xml中,字体将应用于整个应用程序的每个TextView、按钮、EditText等等。在应用程序中使用这种类型的TextView public class MyTextView extends TextView { public MyTextView(Context context, AttributeSet attrs,

我想在我的应用程序中使用自定义字体。这是为整个应用程序提供字体的最佳方式。我知道如何为单个文本视图或按钮指定自定义字体


是否可以在一个地方提及自定义字体,例如在styles.xml中,字体将应用于整个应用程序的每个TextView、按钮、EditText等等。

在应用程序中使用这种类型的TextView

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyTextView(Context context) {
    super(context);
    init();
}

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
    setTypeface(tf ,1);

}

}
还可以编辑文本

public class CEditText extends EditText {


private Context context;
private AttributeSet attrs;
private int defStyle;

public CEditText(Context context) {
    super(context);
    this.context=context;
    init();
} 

 public CEditText(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.context=context;
      this.attrs=attrs;
      init();
 }

public CEditText(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      this.context=context;
      this.attrs=attrs;
      this.defStyle=defStyle;
      init();
}

private void init() {
      Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
      this.setTypeface(font);
}
@Override
public void setTypeface(Typeface tf, int style) {
    tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
    super.setTypeface(tf, style);
}

@Override
public void setTypeface(Typeface tf) {
    tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
    super.setTypeface(tf);
}

不幸的是,android没有提供任何方法在一个地方为整个应用程序应用自定义字体


但是,您可以创建CustomTextView扩展TextView和CustomButton扩展按钮。您可以通过创建一个FontInstance来设置字体。

另外,在布局xml文件中,应该加载我知道的这种方法,谢谢。按钮和编辑文本呢?我需要定制编辑文本和按钮吗?是的,这在android中被称为coustem视图。当人们把别人的答案作为自己的答案发布时,请使用我喜欢的任何xml文件。。。取而代之的是将问题标记为重复,我甚至给它起了个名字:窃取你可以创建自己的主题,并将其应用到清单中的应用程序,但这需要太多时间:可能重复或