适用于整个应用程序的Android Single Custom字体

时间:2016-01-01 06:15:29

标签: android textview custom-font

我正在创建一个应用程序,我想将自定义文本字体设置为所有文本View。当我尝试运行应用程序时,我收到以下错误。我也包含了代码。请仔细研究。

错误:

   :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>

4 个答案:

答案 0 :(得分: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");
}

在yor xml中 -

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

答案 1 :(得分: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>

答案 2 :(得分: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);

答案 3 :(得分: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>