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

时间:2014-06-04 09:08:30

标签: android android-actionbar typeface

我有一个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);
}
}

2 个答案:

答案 0 :(得分: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);

自定义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);
    }
}

只需将上述课程复制到您的项目中,然后在您的代码中实施。

答案 1 :(得分:0)

这有点像黑客,但它确实有效。

protected void onCreate(Bundle savedInstanceState) {
    ...
    int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
    TextView textView = (TextView) findViewById(titleId);
    Typeface typeface = Typeface.create("sans-serif-light", Typeface.ITALIC); // add your typeface
    textView.setTypeface(typeface);
    ...
}