2013-11-20 17 views
0

我試着在下面的帖子前兩個答案改變我的標題的字體: How to Set a Custom Font in the ActionBar Title?和我在模擬器得到消息:在標題修改字體導致錯誤

Unfortunately, DC Parks has stopped. OK 

與展望這裏第二個答案是我的MainActivity代碼:

import android.app.ActionBar; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Spannable; 
import android.text.SpannableString; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    SpannableString s = new SpannableString("My Title"); 
    s.setSpan(new TypefaceSpan(this, "MotorwerkOblique.ttf"), 0, s.length(), 
      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    //Update the action bar title with the TypefaceSpan instance 
    ActionBar actionBar = getActionBar(); 
    actionBar.setTitle(s); 
} 

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

public void test(View view) { 
    //Intent represents the app's 'intent to do something' 
    Intent intent = new Intent(this, NewPage.class); //have to create the new_page activity 
    startActivity(intent); 
} 

public void textclick(View myview) { 
    Intent myintent = new Intent(this, NewPage.class); 
    startActivity(myintent); 
} 
} 

TypefaceSpan.java代碼:

public class TypefaceSpan extends MetricAffectingSpan { 
/** An <code>LruCache</code> for previously loaded typefaces. */ 
private static LruCache<String, Typeface> sTypefaceCache = 
new LruCache<String, Typeface>(12); 

private Typeface mTypeface; 

/** 
* Load the {@link Typeface} and apply to a {@link Spannable}. 
*/ 
public TypefaceSpan(Context context, String typefaceName) { 
mTypeface = sTypefaceCache.get(typefaceName); 

if (mTypeface == null) { 
mTypeface = Typeface.createFromAsset(context.getApplicationContext() 
.getAssets(), String.format("fonts/%s.otf", typefaceName)); 

// Cache the loaded Typeface 
sTypefaceCache.put(typefaceName, mTypeface); 
} 
} 

@Override 
public void updateMeasureState(TextPaint p) { 
p.setTypeface(mTypeface); 
// Note: This flag is required for proper typeface rendering 
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); 
} 

@Override 
public void updateDrawState(TextPaint tp) { 
tp.setTypeface(mTypeface); 
// Note: This flag is required for proper typeface rendering 
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG); 
} 
} 

我的字體位於assens/fonts/MotorwerkObliqhue.ttf

任何幫助將不勝感激!

從控制檯:

[2013-11-20 14:54:58 - DCParks] Dx 
trouble writing output: already prepared 
[2013-11-20 14:55:00 - DCParks] ------------------------------ 
[2013-11-20 14:55:00 - DCParks] Android Launch! 
[2013-11-20 14:55:00 - DCParks] adb is running normally. 
[2013-11-20 14:55:00 - DCParks] Performing com.example.dcparks.MainActivity activity launch 
[2013-11-20 14:55:00 - DCParks] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Nexus4' 
[2013-11-20 14:55:00 - DCParks] Uploading DCParks.apk onto device 'emulator-5554' 
[2013-11-20 14:55:05 - DCParks] Installing DCParks.apk... 
[2013-11-20 14:55:13 - DCParks] Success! 
[2013-11-20 14:55:13 - DCParks] Starting activity com.example.dcparks.MainActivity on device emulator-5554 
[2013-11-20 14:55:16 - DCParks] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.dcparks/.MainActivity } 
+1

檢查您的logcat以獲取更詳細的錯誤。 –

+1

後棧跟蹤。 – njzk2

+0

logcat中的錯誤信息非常長,我實在無法做任何事情。我可以發佈它,但就像我說的那麼久。 – KFP

回答

0

你試圖使用「.TTF」字體文件,但代碼期望的「雜項文件」字型。如果刪除文件路徑的「.otf」部分,它將正常工作。

簡單地改變這樣的:

String.format("fonts/%s.otf", typefaceName)

這樣:

String.format("fonts/%s", typefaceName)

你真的需要雖然發表您的logcat的輸出,它應該顯示實際的錯誤消息。

編輯:詳細說明,在原始代碼中,應用程序正在查找名爲assets/fonts/MotorwerkOblique.ttf.otf的字體文件,該文件可能不存在。它期望你只傳入文件名,不帶擴展名。

+0

我試過字體/%s和字體/%s.ttf,我仍然得到「..停止工作」的消息。如果我刪除Spannable字符串片的應用程序工作正常。 logcat文件太長而無法在這裏發佈。我將它保存到一個.txt文件,但我沒有看到上傳它的方法。我真的希望我能得到這個工作。 – KFP

+0

@KFP您需要找到要發佈的logcat的相應部分。它應該是底部的最後一部分。 – twaddington

+0

@KFP如果您需要將整個logcat從.txt文件複製並粘貼到https://gist.github.com/的要點中。然後只需更新您的原始問題鏈接到要點。 – twaddington