【问题标题】:Getting error in setting custom text style in my application在我的应用程序中设置自定义文本样式时出错
【发布时间】:2014-10-25 07:10:06
【问题描述】:

我在我的 Android 应用程序中为 TextView 和 EditText 使用自定义字体。我为字体编辑文本和文本视图创建了一个单独的类。我正在为我的文本使用 Calibri 字体。

但是在运行我的应用程序后,它在所有文件中都显示以下错误 -

com.pack.demo.MyFontedTextView 实例化失败,其中 MyFontedTextView 是类文件,com.pack.demo.MyFontedEditText 实例化失败。

在我所有的布局中,运行时异常是 -

  java.lang.RuntimeException: native typeface cannot be made
 at android.graphics.Typeface.<init>(Typeface.java:175)
 at android.graphics.Typeface.createFromAsset(Typeface.java:149)
 at com.pack.demo.MyFontedTextView.<init>(MyFontedTextView.java:22)

com.pack.demo.MyFontedTextView.(MyFontedTextView.java:22) 显示字体错误 font = Typeface.createFromAsset(context.getAssets(), "calibri.otf");在下面的类文件中。

这是我的字体编辑文本的代码 -

    public class MyFontedEditText extends EditText {

        public MyFontedEditText(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            Typeface font = Typeface.createFromAsset(context.getAssets(),
                    "calibri.otf");
            this.setTypeface(font);
        }

        public MyFontedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            Typeface font = Typeface.createFromAsset(context.getAssets(),
                    "calibri.otf");
            this.setTypeface(font);
        }

        public MyFontedEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            Typeface font = Typeface.createFromAsset(context.getAssets(),
                    "calibri.otf");
            this.setTypeface(font);
        }
    }

同样,我的字体文本视图代码 -

  public class MyFontedTextView extends TextView {

    public MyFontedTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "calibri.otf");
        this.setTypeface(font);
        this.setTypeface(null, Typeface.BOLD);
    }

    public MyFontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "calibri.otf");
        Log.d("1", "abc");
        this.setTypeface(font);
        this.setTypeface(null, Typeface.BOLD);
    }

    public MyFontedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                "calibri.otf");
        this.setTypeface(font);
        this.setTypeface(null, Typeface.BOLD);
    }
}

【问题讨论】:

  • 你的字体添加到 assets 文件夹了吗?
  • @Sishin..是的,我已经添加了,但仍然无法正常工作..
  • 我认为是拼写错误。将您的 .ttf 文件名检查为您在课堂上提到的名称。
  • 希望你的资产中没有字体文件夹?
  • @Ashutosh 我只是想告诉你检查你的字体没有损坏

标签: android android-layout fonts


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

public class MyFontedTextView extends TextView {

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

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

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

    private void init(Context mContext) {
        try {
            Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "calibri.otf");
            setTypeface(tf,Typeface.BOLD);
        } catch (Throwable e) {
        }
    }
}

【讨论】:

  • 在MainActivity中如何调用上述文件?
  • 你想在 xml 中声明这个 Custom TextView 还是使用运行时?
  • 我也必须在 xml 和运行时使用
  • 很高兴为您提供帮助,如果对您有用,请点赞?
  • 但是亲爱的我没有看到你的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多