TypeFace为TextView, Edittext和按钮给出一个错误



我尝试在TextView上使用TypeFace类实现自定义字体:

package com.royal.bikers;
import android.app.Activity;
import android.os.Bundle;
public class AboutUs extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        TextView aboutUs1 = (TextView) findViewById(R.id.aboutUs1);
        TextView aboutUs2 = (TextView) findViewById(R.id.aboutUs2);
        TextView aboutUs3 = (TextView) findViewById(R.id.aboutUs3);
        TextView aboutUs4 = (TextView) findViewById(R.id.aboutUs4);
        TextView aboutUs5 = (TextView) findViewById(R.id.aboutUs5);
        TextView aboutUsTitle = (TextView) findViewById(R.id.aboutUsTitle);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
        aboutUs1.setTypeface(tf);
        aboutUs2.setTypeface(tf);
        aboutUs3.setTypeface(tf);
        aboutUs4.setTypeface(tf);
        aboutUs5.setTypeface(tf);
        aboutUsTitle.setTypeface(tf);
    }
}

但是程序崩溃了:

05-09 13:42:13.525: E/AndroidRuntime(24251): FATAL EXCEPTION: main
05-09 13:42:13.525: E/AndroidRuntime(24251): Process: com.royal.bikers, PID: 24251
05-09 13:42:13.525: E/AndroidRuntime(24251): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.royal.bikers/com.royal.bikers.AboutUs}: java.lang.RuntimeException: native typeface cannot be made
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.os.Looper.loop(Looper.java:136)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at java.lang.reflect.Method.invoke(Native Method)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-09 13:42:13.525: E/AndroidRuntime(24251): Caused by: java.lang.RuntimeException: native typeface cannot be made
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.graphics.Typeface.<init>(Typeface.java:175)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.graphics.Typeface.createFromAsset(Typeface.java:149)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at com.royal.bikers.AboutUs.onCreate(AboutUs.java:23)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.Activity.performCreate(Activity.java:5231)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-09 13:42:13.525: E/AndroidRuntime(24251):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-09 13:42:13.525: E/AndroidRuntime(24251):    ... 9 more

我曾尝试使用另一种字体叫做Usuzi,它工作得很好,但我想把它改成Roboto-Thin,它不工作。


这是我的 about .xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/aboutUs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ic_bg"
    tools:context="com.royal.bikers.AboutUs"
    tools:ignore="MergeRootFrame" >
    <TextView
        android:id="@+id/aboutUsTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="65dp"
        android:gravity="center_horizontal"
        android:text="@string/aboutUs"
        android:textColor="#000"
        android:textSize="25sp"
        android:textStyle="bold" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="105dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="110dp"
        android:background="#59FFFFFF" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/aboutUs1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/aboutUs1"
                android:textColor="#000"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/aboutUs2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="30dp"
                android:text="@string/aboutUs2"
                android:textColor="#000"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/aboutUs3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="30dp"
                android:text="@string/aboutUs3"
                android:textColor="#000"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/aboutUs4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="30dp"
                android:text="@string/aboutUs4"
                android:textColor="#000"
                android:textSize="20sp" />
            <TextView
                android:id="@+id/aboutUs5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:text="@string/aboutUs5"
                android:textColor="#000"
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.royal.bikers"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar" >
        <activity
            android:name="com.royal.bikers.Splash"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.royal.bikers.Home"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.Login"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.Register"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.AboutUs"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.Search"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.RegisterEvent"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.Event"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.Account"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
        <activity
            android:name="com.royal.bikers.ChangePassword"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden" >
        </activity>
    </application>
</manifest>


请建议我,如果有任何其他方法除了创建CustomTextView类扩展TextView作为给定的这里

那么试试这个,可能是流问题

public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
    synchronized (cache) {
        if (!cache.containsKey(assetPath)) {
            try {
                Typeface t = Typeface.createFromAsset(c.getAssets(),
                        assetPath);
                cache.put(assetPath, t);
            } catch (Exception e) {
                Log.e(TAG, "Could not get typeface '" + assetPath
                        + "' because " + e.getMessage());
                return null;
            }
        }
        return cache.get(assetPath);
    }
}

}

这就是答案,只需删除字体字符串,只使用字体名称。

Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Thin.ttf");

自定义fontloader

public class FontLoader {
private static Typeface quickSandTypeface = null;
private static Typeface quickSandTypefaceBold = null;
public static void loadFonts(Context context) {
    quickSandTypeface = Typeface.createFromAsset(context.getAssets(), "quicksand_regular.ttf");
    quickSandTypefaceBold = Typeface.createFromAsset(context.getAssets(), "quicksand_bold.ttf");
}
public static Typeface getQuickSandTypeface() {
    return quickSandTypeface;
}
public static Typeface getQuickSandTypefaceBold() {
    return quickSandTypefaceBold;
}
public static void setQuickSandTypeface(EditText... editTextViews) {
    if (null != editTextViews && null != quickSandTypeface) {
        for (int i = 0; i < editTextViews.length; i++) {
            editTextViews[i].setTypeface(getQuickSandTypeface());
        }
    }
}

在你的活动中这样使用

FontLoader.setQuickSandTypeface (yourEditText)

试试这个,它会帮到你的。

        Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/helvetica.ttf");

ttf文件应该用小写字母。

相关内容

最新更新