如何将自定义字体设置为sherlock的上下文操作栏?

Mic*_*ith 5 android actionbarsherlock

我知道如何将自定义字体设置为操作栏.我只需要扩展SherlockFragmentActivity并覆盖setTitle,如下所示:

@Override
public void setTitle(CharSequence title) {
    String str = String.valueOf(title);
    str = str.toUpperCase(Locale.getDefault());
    SpannableString s = new SpannableString(str);
    MetricAffectingSpan span = new MetricAffectingSpan() {
        @Override
        public void updateMeasureState(TextPaint p) {
            p.setTypeface(FontManager.INSTANCE.getAppFont());
        }

        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setTypeface(FontManager.INSTANCE.getAppFont());
        }
    };

    s.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    getSupportActionBar().setTitle(s);
}
Run Code Online (Sandbox Code Playgroud)

但是,使用上下文操作栏会使事情变得复杂.该库使用工厂返回上下文操作栏,如下所示:

ActionMode mode = getSherlockActivity().startActionMode(mActionModeCallback);
mode.setTitle("whatever");
Run Code Online (Sandbox Code Playgroud)

我可以覆盖ActionMode,但lib不会返回它.

有任何想法吗?

Jor*_*Gil 4

我看有点复杂...

您将需要创建一个View放置TextView标题的位置,设置所需的字体TextView,并使用setCustomView新字体放置视图。

我希望它对你有帮助。

更新

您是否尝试过创建自己的方法,如下所示:

public void setActionModeTitle(CharSequence title) {
    String str = String.valueOf(title);
    str = str.toUpperCase(Locale.getDefault());
    SpannableString s = new SpannableString(str);
    MetricAffectingSpan span = new MetricAffectingSpan() {
        @Override
        public void updateMeasureState(TextPaint p) {
            p.setTypeface(FontManager.INSTANCE.getAppFont());
        }

        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setTypeface(FontManager.INSTANCE.getAppFont());
        }
    };

    s.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    actionMode.setTitle(s);
}
Run Code Online (Sandbox Code Playgroud)