8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidAdvent Calendar 2015

Day 8

NavigationViewのFontを変更する

Last updated at Posted at 2015-12-07

advent_carender_0.png

Android Advent Calendar 8日目の投稿です。
こんにちは。@hi6484です。
Goodpatchで日々Androidアプリを作ってます。

android.support.design.widget.NavigationViewのFont Typeを変更させるためのTipsです。
NavigationViewの実装方法などはAndroid SDKのSampleアプリなどを確認してください。

#Fontを設定しないでNavigationViewを実装した場合。
Screenshot_20151207-155352.png

英数字は問題ないが、日本語文字が端末によっては・・・。ってなる。

#Fontを設定してNavigationViewを実装した場合。(DancingScript-Bold.ttf)
Screenshot_20151207-155444.png

#実装方法(抜粋)

getViewTreeObserver().addOnGlobalLayoutListener()にて
NavigationViewが作成されたタイミングを取得する
android.text.SpannableStringに設定したいTypefaceや文字列を設定する。

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.nav_view)
    NavigationView mNavigationView;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mContext = getApplicationContext();
        mNavigationView.getViewTreeObserver().addOnGlobalLayoutListener(
                mOnGlobalLayoutListener);
    }

    private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener =
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    AssetManager assetManager = mContext.getAssets();
                    Typeface typeface = Typeface.createFromAsset(assetManager,
                            "DancingScript-Bold.ttf");
                    mNavigationView.getViewTreeObserver().
                            removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
                    Menu menu = mNavigationView.getMenu();
                    for (int loop = 0; loop < menu.size(); loop++) {
                        MenuItem item = menu.getItem(loop);
                        //外部フォントの設定.
                        SpannableString title =
                                new SpannableString(item.getTitle().toString());
                        title.setSpan(new CustomTypefaceSpan("" , typeface), 0 ,
                                title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

                        item.setTitle(title);
                        SubMenu subMenu = item.getSubMenu();
                        if (subMenu != null) {
                            for (int subLoop = 0; subLoop < subMenu.size(); subLoop++) {
                                MenuItem subItem = subMenu.getItem(subLoop);
                                //外部フォントの設定.
                                SpannableString subTitle =
                                        new SpannableString(item.getTitle().toString());
                                subTitle.setSpan(new CustomTypefaceSpan("" , typeface), 0 ,
                                        subTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                                subItem.setTitle(subTitle);
                            }
                        }
                    }
                }
            };
}

あとは、CustomTypefaceSpan classを作成してあげればOK.

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface mTypeface;

    public CustomTypefaceSpan(String family, Typeface typeface) {
        super(family);
        mTypeface = typeface;
    }

    @Override
    public void updateDrawState(TextPaint textPaint) {
        applyCustomTypeFace(textPaint, mTypeface);
    }

    @Override
    public void updateMeasureState(TextPaint textPaint) {
        applyCustomTypeFace(textPaint, mTypeface);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface typeface) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~typeface.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(typeface);
    }
}

CustomTypefaceSpanのfull codeは下記を参照してください。
https://android.googlesource.com/platform/packages/apps/UnifiedEmail/+/kitkat-release/src/com/android/mail/ui/CustomTypefaceSpan.java

#SampleCode
SampleAdventCalendar2015

#参考
How to set custom typeface to items in NavigationView?

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?