2016-01-01 36 views
0

我正在创建一个应用程序,我要将自定义文本字体设置为所有文本Views.When我试图运行该应用程序时出现以下错误。我也包括了代码。请看看它。整个应用程序的Android单个自定义字体

错误:

:39:59.899 31667-31667/com.example.jobinsabu.textviewfont E/AndroidRuntime: FATAL EXCEPTION: main 
                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobinsabu.textviewfont/com.example.jobinsabu.textviewfont.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class com.example.jobinsabu.textviewfont.TextRoman 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306) 
                         at android.app.ActivityThread.access$700(ActivityThread.java:153) 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286) 
                         at android.os.Handler.dispatchMessage(Handler.java:99) 
                         at android.os.Looper.loop(Looper.java:176) 
                         at android.app.ActivityThread.main(ActivityThread.java:5302) 
                         at java.lang.reflect.Method.invokeNative(Native Method) 
                         at java.lang.reflect.Method.invoke(Method.java:511) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                         at dalvik.system.NativeStart.main(Native Method) 
                        Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class com.example.jobinsabu.textviewfont.TextRoman 
                         at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693) 
                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:752) 
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:495) 
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                         at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                         at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                         at com.example.jobinsabu.textviewfont.MainActivity.onCreate(MainActivity.java:18) 
                         at android.app.Activity.performCreate(Activity.java:5326) 
                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)  
                         at android.app.ActivityThread.access$700(ActivityThread.java:153)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)  
                         at android.os.Handler.dispatchMessage(Handler.java:99)  
                         at android.os.Looper.loop(Looper.java:176)  
                         at android.app.ActivityThread.main(ActivityThread.java:5302)  
                         at java.lang.reflect.Method.invokeNative(Native Method)  
                         at java.lang.reflect.Method.invoke(Method.java:511)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)  
                         at dalvik.system.NativeStart.main(Native Method)  
                        Caused by: java.lang.reflect.InvocationTargetException 
                         at java.lang.reflect.Constructor.constructNative(Native Method) 
                         at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
                         at android.view.LayoutInflater.createView(LayoutInflater.java:593) 
                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)  
                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)  
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:495)  
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:397)  
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:353)  
                         at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)  
                         at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)  
                         at com.example.jobinsabu.textviewfont.MainActivity.onCreate(MainActivity.java:18)  
                         at android.app.Activity.performCreate(Activity.java:5326)  
                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)  
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)  
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)  
                         at android.app.ActivityThread.access$700(ActivityThread.java:153)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)  
                         at android.os.Handler.dispatchMessage(Handler.java:99)  
                         at android.os.Looper.loop(Looper.java:176)  
                         at android.app.ActivityThread.main(ActivityThread.java:5302)  
                         at java.lang.reflect.Method.invokeNative(Native Method)  
                         at java.lang.reflect.Method.invoke(Method.java:511)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)  
                         at dalvik.system.NativeStart.main(Native Method)  
                        Caused by: java.lang.RuntimeException: native typeface cannot be made 
                         at android.graphics.Typeface.<init>(Typeface.java:321) 
                         at android.graphics.Typeface.createFromAsset(Typeface.java:295) 
                         at com.example.jobinsabu.textviewfont.TextRoman.init(TextRoman.java:26) 
                         at com.example.jobinsabu.textviewfont.TextRoman.<init>(TextRoman.java:22) 
                         at java.lang.reflect.Constructor.constructNative(Native Method)  
                         at java.lang.reflect.Constructor.newInstance(Constructor.java:417)  
                         at android.view.LayoutInflater.createView(LayoutInflater.java:593)  
                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)  
                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)  
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:495)  
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:397)  
                         at android.view.LayoutInflater.inflate(LayoutInflater.java:353)  
                         at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)  
                         at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)  
                         at com.example.jobinsabu.textviewfont.MainActivity.onCreate(MainActivity.java:18)  
                         at android.app.Activity.performCreate(Activity.java:5326)  
                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)  
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)  
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)  
                         at android.app.ActivityThread.access$700(ActivityThread.java:153)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)  
                         at android.os.Handler.dispatchMessage(Handler.java:99)  
                         at android.os.Looper.loop(Looper.java:176)  
                         at android.app.ActivityThread.main(ActivityThread.java:5302)  
                         at java.lang.reflect.Method.invokeNative(Native Method)  
                         at java.lang.reflect.Method.invoke(Method.java:511)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)  
                         at dalvik.system.NativeStart.main(Native Method)  

TextRoman.java:

package com.example.jobinsabu.textviewfont; 

/** 
* Created by Jobin Sabu on 12/31/2015. 
*/ 
import android.content.Context; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 
/** 
* this class is used for set textview font. 
* @author abc 
* 
*/ 
public class TextRoman extends TextView{ 
    Context context; 

    String TAG = getClass().getName(); 
    public TextRoman(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
     init(); 
    } 

    private void init() { 
     Typeface font = Typeface.createFromAsset(context.getAssets(), "A771-Roman-Regular.ttf"); 

     setTypeface(font); 
    } 

    @Override 
    public void setTypeface(Typeface tf) { 
     super.setTypeface(tf); 
    } 

} 

MainActivity.java:

package com.example.jobinsabu.textviewfont; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 
Context context=this; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 


    <com.example.jobinsabu.textviewfont.TextRoman 
     android:id="@+id/txt_ItemName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Roman Rules" 
     android:textColor="#EAA55A" 
     android:textSize="22sp" /> 
</android.support.design.widget.CoordinatorLayout> 
+0

请参阅此http://stackoverflow.com/a/16883281/4494555 – Jas

+0

误差在初始化你的字体。您是否直接在资产文件夹或类似资产/字体中存储您的ttf –

+0

字体直接位于资产文件夹内。我没有在资产内创建子文件夹 – jobin

回答

1

尝试我 -

package com.example.customviews; 
    import android.content.Context; 
    import android.util.AttributeSet; 
    import android.widget.TextView; 

public class CustomTextView extends TextView { 

public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    setTypeface(Utils.getTypeFace(context)); 
} 

public CustomTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setTypeface(Utils.getTypeFace(context)); 
} 

public CustomTextView(Context context) { 
    super(context); 
    setTypeface(Utils.getTypeFace(context)); 
} 

这一点,工作}

public class Utils { 
public static Typeface getTypeFace(Context context) { 
    return Typeface.createFromAsset(context.getAssets(),"Arail.ttf"); 
} 

而在侑XML的

 <com.example.customviews.CustomTextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
+0

我试过了您给我的代码。取而代之Arial.ttf我使用A771- Roman-Regular.ttf,但我得到以下渲染问题 – jobin

0

你可以做这样的事情..

<resources> 
<style name="MyFont" parent="@android:style/TextAppearance.Medium"> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:textColor">your_color</item> 
    <item name="android:typeface">name of your_custom font</item> 
</style> 

0

它的工作对我来说

Textiew textView = (TextView) toolbar.findViewById(R.id.toolbar_title); 
Typeface typeFace =Typeface.createFromAsset(getAssets(),Constants.TYPEFACE_SOLAIMAN_LIPI); 

    textView.setTypeface(typeFace); 
    textView.setText(getString(R.string.welcome)); 
    setSupportActionBar(toolbar); 
0

是与反思。这工作:

import java.lang.reflect.Field; 
import android.content.Context; 
import android.graphics.Typeface; 

public final class FontsOverride { 

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

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

然后,您需要重载几个默认字体,例如在应用程序类:

public final class Application extends android.app.Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf"); 
     FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf"); 
     FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf"); 
     FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf"); 
    } 
} 

或者当然,如果你使用的是相同的字体文件,则可以改善这加载一次。

不过,我倾向于只覆盖一个,说 “MONOSPACE”,然后设置一个样式,迫使该字体的字体应用广泛:

<resources> 
<style name="AppBaseTheme" parent="android:Theme.Light"> 
</style> 

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 
    <item name="android:typeface">monospace</item> 
</style> 

API 21的是Android 5.0 我已经调查在评论中的报告,它不工作,它似乎与主题android:Theme.Material.Light不兼容。

如果这个主题是不是对你很重要,使用较旧的主题,例如:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
    <item name="android:typeface">monospace</item> 
</style> 
相关问题