gpt4 book ai didi

java - 如何为我的整个应用程序设置默认手机字体

转载 作者:行者123 更新时间:2023-11-28 06:23:59 25 4
gpt4 key购买 nike

我想知道是否有办法为我的应用程序设置默认手机字体。此时,我的应用程序从我的 LG G2(纯白色字体)读取字体,并将其仅设置在对话框、 snackbar 和 toast 上,而不是在 TextView 或微调器中。如何将字体传递给 TextView 和微调器?

enter image description here

enter image description here

最佳答案

有几种方法可以做到这一点。

来自 this answer ,您可以创建自定义 View ,例如:

public class YourTextView extends TextView {

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

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

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

private void init() {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/helveticaneue.ttf");
setTypeface(tf);
}
}

然后您可以简单地在您的布局中使用此 TextView 。主要好处是您不必为它们单独指定字体。

如果您确实想单独指定字体,那么:

 TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);

如果您更愿意使用图书馆,请查看 Calligraphy ,它完成了所有这些以及更多。

关于java - 如何为我的整个应用程序设置默认手机字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35390814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com