【问题标题】:How can I set Typeface in action bar? [duplicate]如何在操作栏中设置字体? [复制]
【发布时间】:2014-07-24 20:37:13
【问题描述】:

我有一个Activity,它在其他活动中从ListView 位置获得ActionBar 标题,我想在ActionBar 中设置Typeface

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

【问题讨论】:

标签: android android-actionbar typeface


【解决方案1】:

这有点小技巧,但它确实有效。

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

【讨论】:

  • 谢谢,但我试了一下,它不起作用。如果你能给我完整的代码会更好..对不起。
  • 我编辑了答案。这个例子对我有用。
  • 我在 Assets/fonts 中有字体资源存储,但是从 Assets 获取字体时出现错误。
【解决方案2】:

我们必须使用自定义的 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 目录中的字体名称。它加载文件并在内存中缓存一个新的字体实例。 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);
    }
}

只需将上述类复制到您的项目中并在您的代码中实现即可。

【讨论】:

  • 我尝试使用它,但此链接出错:s.setSpan(new TypefaceSpan(this, "fonts/khmerbibleregular.ttf"), 0, s.length(),
  • 请问是什么错误?
  • 从 Assets 获取字体时出错。
  • 我编辑了我的回复。请尝试一下。
  • 非常感谢你终于我做到了,它现在正在工作.. :D 再次感谢你。抱歉问太多了……
猜你喜欢
  • 2016-08-30
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多