【问题标题】:Custom font used in entire app整个应用程序中使用的自定义字体
【发布时间】:2013-10-22 01:34:51
【问题描述】:

因此,在 Android 中设置字体有不同的方法。 我需要做的是将字体与custom font 组合设置,从而覆盖entire app

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

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

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

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

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

【问题讨论】:

    标签: android fonts


    【解决方案1】:

    据我所知,我们无法访问 xml 文件中的外部资产。

    【讨论】:

    • 同时我也没有找到解决这个问题的任何方法。我只是使用自定义 Textview。希望将来会有这方面的东西。它使代码更加简洁。
    【解决方案2】:

    试试这个代码:

        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);
    
    }
    
      }
    

    然后在 XML 中使用 ;

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

    【讨论】:

    • 这不是我的问题。这是我的第一个选择。因此,您覆盖了 textview 而不是我提到的 edittext。但除此之外,我知道这个选项,在我看来,它不是最有效的方式。这就像一个计划b。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2011-11-28
    • 2011-09-13
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多