如何在操作栏中设置字体?

use*_*962 4 android typeface android-actionbar

我有一个从其他活动的位置Activity获得ActionBar头衔ListView,我想要Typeface进入ActionBar.

package com.cambobox.actionbartitle.actionbar;

import android.app.ActionBar;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TypefaceSpan;
import android.widget.TextView;

public class Song extends ActionBarActivity {
Typeface font;
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_song);
   font = Typeface.createFromAsset(getAssets(),"fonts/khmerbibleregular.ttf");
   Intent intent = getIntent();
   String listview_id = intent.getStringExtra(SongList.NAME);
   ActionBar actionnbar_title = getActionBar();
   actionnbar_title.setDisplayShowTitleEnabled(true);
   actionnbar_title.setTitle(font);
   actionnbar_title.setTitle(listview_id);
}
}
Run Code Online (Sandbox Code Playgroud)

Far*_*uzi 7

我们必须使用自定义TypefaceSpan类.

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

    // Update the action bar title with the TypefaceSpan instance
    ActionBar actionBar = getActionBar();
    actionBar.setTitle(s);
Run Code Online (Sandbox Code Playgroud)

自定义TypefaceSpan类将传递您的Activity上下文和Assets/fonts目录中的字体名称.它加载文件并在内存中缓存一个新的Typeface实例.TypefaceSpan的完整实现非常简单:

/**
 * Style a {@link Spannable} with a custom {@link Typeface}.
 * 
 * @author Tristan Waddington
 */
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", 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

只需将上面的类复制到项目中并在代码中实现它.