android - 整个应用程序中使用的自定义字体

标签 android fonts

所以在 Android 中有不同的方法来设置你的字体。 我需要做的是将字体与 自定义字体 结合使用,从而覆盖 整个应用程序

现在经过一些研究,我有 2 个选择。

  • 覆盖 EditText 并设置字体,在我的布局 xml 中使用此类。
  • 在我的 styles.xml 中为我的 AppTheme 添加字体。

现在我更喜欢最后一个选项,这显然是我的最佳选择,因为我在整个应用程序中使用相同的字体。 现在的问题是,在我的 Assets/font/ 文件夹中,我添加了我想使用的字体,但我不能在我的样式中使用它。

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:typeface"> Here? </item>
</style>

如何将我的字体添加为字样。这可能吗?

最佳答案

试试这段代码:

    public class CustomTextView extends EditText {

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (!isInEditMode())
        init();

}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (!isInEditMode())
        init();

}

public CustomTextView(Context context) {
    super(context);
    if (!isInEditMode())
        init();
}

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), Constants.FONTPATH);
    setTypeface(tf);

}

  }

然后在XMLwith中使用;

     <pkgname.CustomTextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content"  /> 

关于android - 整个应用程序中使用的自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361036/

相关文章:

android - DaggerIssueRegistry 未指定供应商

android - Android 中的相机预览不是全屏

android - 如何在android中使用Volley库上传文件?

android - TabHost UI 布局问题

java - 在 Android studio 中使用 otf 字体

android - Jenkins Git 发布者 : How to commit code back to master after build?

fonts - 如何增加 Google colab 单元格输出的字体大小?

c# - 在 DrawText Wpf C# 中使用自定义字体

java - 如何使用 Java 查找机器上是否正确安装了某种字体

java - UTF-8 字符显示不正确