字体覆盖对LayoutInflater没有影响

时间:2017-07-11 15:16:11

标签: android textview layout-inflater true-type-fonts

覆盖字体AllOver我使用的应用程序

 public class TypefaceOverride {

  public static void override(Context context, String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
  }

  private static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
    try {
      final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
      staticField.setAccessible(true);
      staticField.set(null, newTypeface);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

然后在应用程序类

上使用此方法
private void typeFace() {
    TypefaceOverride.override(this, "DEFAULT", "iran_sans.ttf");
    TypefaceOverride.override(this, "MONOSPACE", "iran_sans.ttf");
    TypefaceOverride.override(this, "SERIF", "iran_sans.ttf");
    TypefaceOverride.override(this, "SANS_SERIF", "iran_sans.ttf");
  }

所有字体也会改变,但是当我

 LinearLayout product = (LinearLayout) G.inflater.inflate(R.layout.product, null);
 TextView txt = (TextView) product.findViewById(R.id.txt);

TextView字体不能获得效果我必须在其上设置字体

  txt.setTypeface(Typefaces.get(G.context, "iran_sans.ttf"));

此外,LinearLayout中的RecycleView膨胀但 colorEdgeEffect 颜色错误(灰色!)

我该怎么办?

1 个答案:

答案 0 :(得分:0)

该死的教育! ,为什么我的英语太糟糕了 终于找到了解决方案

https://developer.android.com/reference/android/view/LayoutInflater.html

只需添加上下文

   LinearLayout product = (LinearLayout) G.inflater.cloneInContext(ActivityMain.this).inflate(R.layout.product, null);