新的TypefaceSpan(Typeface)的Android TypefaceSpan NoSuchMethodError

时间:2018-08-25 01:03:32

标签: android fonts android-typeface

我正在尝试从库中对#!/usr/bin/env python3 # # A buggy web service in need of a database. from flask import Flask, request, redirect, url_for from forumdb import get_posts, add_post app = Flask(__name__) # HTML template for the forum page HTML_WRAP = '''\ <!DOCTYPE html> <html> <head> <title>DB Forum</title> <style> h1, form { text-align: center; } textarea { width: 400px; height: 100px; } div.post { border: 1px solid #999; padding: 10px 10px; margin: 10px 20%%; } hr.postbound { width: 50%%; } em.date { color: #999 } </style> </head> <body> <h1>DB Forum</h1> <form method=post> <div><textarea id="content" name="content"></textarea></div> <div><button id="go" type="submit">Post message</button></div> </form> <!-- post content will go here --> %s </body> </html> ''' # HTML template for an individual comment POST = '''\ <div class=post><em class=date>%s</em><br>%s</div> ''' @app.route('/', methods=['GET']) def main(): '''Main page of the forum.''' posts = "".join(POST % (date, text) for text, date in get_posts()) html = HTML_WRAP % posts return html @app.route('/', methods=['POST']) def post(): '''New post submission.''' message = request.form['content'] add_post(message) return redirect(url_for('main')) if __name__ == '__main__': app.run(host='0.0.0.0', port=8000) 应用自定义字体,并且字体文件存储在app文件夹的res / font中。

我使用

获得了字体
TextView

字体不为空,我已经放置了调试点并进行了验证。

int id = context.getResources.getIdentifier("xxx","font",packageName);
Typeface typeface = context.getResources.getFont(id);

现在,我想从此字体创建TypefaceSpan对象,但出现错误并导致应用程序崩溃。

  

java.lang.NoSuchMethodError:没有直接方法   (Landroid / graphics / Typeface;)V在课堂上   Landroid / text / style / TypefaceSpan;或其超类(声明为   “ android.text.style.TypefaceSpan”出现在   /system/framework/framework.jar!classes2.dex)。

请帮助。 预先感谢。

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,这个answer对我有用

您需要创建CustomTypefaceSpan

package com.rytalo.rtaxi.presentation.ui.main.selectlocation;

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

   private final Typeface newType;

   public CustomTypefaceSpan(String family, Typeface type) {
     super(family);
     newType = type;
   }

   @Override
   public void updateDrawState(TextPaint ds) {
     applyCustomTypeFace(ds, newType);
   }

   @Override
   public void updateMeasureState(TextPaint paint) {
     applyCustomTypeFace(paint, newType);
   }

   private static void applyCustomTypeFace(Paint paint, Typeface tf) {
     int oldStyle;
     Typeface old = paint.getTypeface();
     if (old == null) {
       oldStyle = 0;
     } else {
       oldStyle = old.getStyle();
     }

     int fake = oldStyle & ~tf.getStyle();
     if ((fake & Typeface.BOLD) != 0) {
       paint.setFakeBoldText(true);
     }

     if ((fake & Typeface.ITALIC) != 0) {
       paint.setTextSkewX(-0.25f);
     }

     paint.setTypeface(tf);
   }
 }

然后像这样使用它

TypeFace face = ResourcesCompat.getFont(context, R.font.font_name); // in case you want to support below API 26.
new CustomTypefaceSpan("", face);

答案 1 :(得分:1)

尝试重现您的代码时,出现以下错误:
对于getFont()

  

调用需要API级别26


对于TypefaceSpan span = new TypefaceSpan(typeface);

  

调用需要API级别27

您必须至少拥有以下内容:

compileSdkVersion 27
implementation 'com.android.support:appcompat-v7:27.1.1'