2014-05-09 22 views
0

我嘗試使用字體類這樣來實現自定義字體上的TextView:字體爲TextView的,EDITTEXT和按鈕給人一種錯誤

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並且它不適用於它。


這裏是我的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> 

這裏的清單

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


請給我建議,如果有除創建延伸的TextView作爲CustomTextView類的任何其他方法給定here

+0

確定'中的Roboto-Thin.ttf'可用你的'資產文件夾? –

+0

是的,我在那裏。此方法與** usuzi **字體一起使用,但不適用於任何roboto字體。 – Chipset

+0

將名稱從「Roboto-Thin.ttf」更改爲「Robotothin.ttf」,並嘗試爲此重新卸載應用安裝應用程序 –

回答

-1

試試這個會幫你。

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

ttf文件應該是小寫字母。

+0

我正在使用這種方法,但它使用一種字體而不與其他工作。我試圖通過從'Roboto-Thin.ttf'將字體名稱更改爲'rthin.ttf',仍然是同樣的問題。 – Chipset

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

}

0

這裏就是答案只是刪除字體串並只使用字體名稱。

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

這不起作用,因爲資產位於字體文件夾內。 – Chipset

+0

只需將您的ttf文件放入資產中即可。 –

+0

這仍然會導致錯誤 – Chipset

0

使定製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)