TypeFace for TextView,Edittext和Button给出错误

时间:2014-05-09 09:13:20

标签: java android textview typeface

我尝试使用TypeFace类在TextView上实现自定义字体,如下所示:

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 ,而且它不是&#39;与...合作。

这是我的 AboutUs.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>

这里是 Manifest

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

除了创建自定义TextTextView类以及 here

的TextView扩展TextView之外,请建议我是否有任何其他方法

4 个答案:

答案 0 :(得分:0)

好的,然后尝试这个,可能是它的流问题

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);
    }
}

}

答案 1 :(得分:0)

这里的答案就是删除字体字符串并仅使用字体名称。

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

答案 2 :(得分:0)

制作自定义字体装载程序

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)

答案 3 :(得分:-1)

试试这个它会帮助你。

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

ttf文件应该是小写字母。