How to set typeface in action bar?

I have one Activitythat gets the ActionBartitle from ListViewin another activity, and I want to set Typefaceto 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);
}
}
+4
source share
2 answers

We must use our own TypefaceSpan class.

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

The custom TypefaceSpan class passes your Activity context and the font name in your Assets / fonts directory. It downloads the file and caches a new instance of Typeface in memory. The full implementation of TypefaceSpan is surprisingly simple:

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

Just copy the above class into your project and paste it into your code.

+7
source

This is a bit hacky, but it works.

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

Source: https://habr.com/ru/post/1543183/


All Articles