修改标题中的字体会导致错误

时间:2013-11-20 19:23:47

标签: java android eclipse

我在以下帖子中尝试了前两个答案来更改标题的字体: 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);
}
}

我的字体位于assests / 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 个答案:

答案 0 :(得分:0)

您正在尝试使用“.ttf”字体文件,但代码需要“.otf”字体。如果删除文件路径的“.otf”部分,它将正常工作。

只需改变一下:

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

到此:

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

你真的需要发布你的logcat输出,因为它应该显示实际的错误信息。

编辑:详细说明,在原始代码中,应用程序正在寻找一个名为assets/fonts/MotorwerkOblique.ttf.otf的字体文件,该文件可能不存在。它希望您只传入文件名,不带扩展名。